Skip to content

Automatically Extract Title from First Heading in Markdown #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Alberthor47
Copy link
Contributor

📋 Automatically Extract Title from First Heading in Markdown

📝 Description

This pull request introduces functionality to automatically extract the title from the first heading in the Markdown document, prioritizing the highest hierarchy (h1 > h2 > h3, etc.), when no title is provided via global directives (marpCLITitle). It ensures that the title is always set appropriately, improving the user experience for slide generation.

🔄 Changes

  1. Added the extractTitleFromFirstHeading function to info-plugin.ts:
    • Iterates through Markdown tokens to find the first heading based on hierarchy.
    • Skips malformed or empty headings.
    • Stops searching early when an h1 is found for performance.
  2. Updated the infoPlugin to use the extracted title as a fallback when no global directive title is provided.
  3. Ensured robust handling of edge cases, such as:
    • No headings in the document.
    • Empty or malformed headings.
    • Multiple headings at the same level.

🎯 Purpose

The primary purpose of this pull request is to enhance the title extraction logic for Marp CLI. It ensures that users who do not explicitly provide a title via global directives still have a meaningful title extracted from their Markdown content, improving the overall usability of the tool.

💬 Additional Comments

Let me know if further adjustments are needed! 🚀

Related Issues

Fixes #131

@yhatt yhatt self-requested a review May 15, 2025 10:09
@yhatt
Copy link
Member

yhatt commented May 17, 2025

Thanks for your working 😄 Could you add the test code for that?

For running code check with CI, please fetch and merge upstream branch main. (I've worked to fix up reported audits on #657)

@Alberthor47
Copy link
Contributor Author

For sure, working on it!

@Alberthor47
Copy link
Contributor Author

I'm probably missing something, but Idk why I'm failing check in CI

Screenshot 2025-05-21 at 17 40 01

@yhatt
Copy link
Member

yhatt commented May 23, 2025

@Alberthor47
Copy link
Contributor Author

I see, I had conflicts with my local rules. Thanks for the heads up!

headingContentToken.type === 'inline' &&
(!bestHeading || level < bestHeading.level)
) {
bestHeading = { level, content: headingContentToken.content.trim() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the initial release is fine with this. In future releases, it would be better to use MarkdownIt.renderInline() to reflect the exact contents.

Comment on lines +4 to +5
// Helper to create mock heading tokens
const createHeadingTokens = (tag: string, content: string) => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to test with the result of parse by actual Marp or markdown-it instance.

You're right, haven't though that would be something desired.

Co-authored-by: Yuki Hattori <[email protected]>
@yhatt
Copy link
Member

yhatt commented Jun 11, 2025

We've relaxed failure of CI due to vulnerability of depending packages (#663). To merge, an updated condition d6f0682 should pass CI. Sorry for the inconvenience but please fetch and merge upstream branch again.

@Alberthor47
Copy link
Contributor Author

No worries, I'll like to see this feature so whatever is needed to merge the pr 😄

@yhatt yhatt merged commit de415c7 into marp-team:main Jun 11, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect the title of deck from first slide when title option is not passed
2 participants