diff --git a/gatsby-node.js b/gatsby-node.js index bec853b1..4ad23e8a 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -778,10 +778,16 @@ const createIndex = async (nodes, pages) => { const getTitle = (pages, slug, node) => { let title = searchTree(pages, slug) || node.frontmatter?.title if (!title) { - const firstLine = node?.rawMarkdownBody?.split("\n", 1)[0] - title = firstLine?.replace(/#/g, "") + const headingRegex = /^#+\s+(.*)/m // match Markdown heading + const match = node?.rawMarkdownBody?.match(headingRegex) + title = match + ? match[1] + : node?.rawMarkdownBody + ?.replace(//gs, "") + .match(/^.+$/m)?.[0] // match first non-empty line + .trim() } - return title?.replace(//gs, "") + return title } exports.sourceNodes = async ({