@@ -1370,8 +1370,7 @@ export function analyzeTemplateStructure(templateData: string): {
13701370 inlineTitleText : string ,
13711371} {
13721372 try {
1373- logDebug ( 'analyzeTemplateStructure' , `Analyzing template structure for template with ${ templateData . length } characters` )
1374-
1373+ logDebug ( 'analyzeTemplateStructure' , `Analyzing template structure for template with ${ templateData . length } characters, starting with "${ templateData . substring ( 0 , 20 ) } ..."` )
13751374 // Initialize return object
13761375 const result = {
13771376 hasNewNoteTitle : false ,
@@ -1397,6 +1396,7 @@ export function analyzeTemplateStructure(templateData: string): {
13971396 }
13981397 }
13991398 }
1399+ logDebug ( 'analyzeTemplateStructure ', `templateFrontmatterEnd : ${templateFrontmatterEnd } `)
14001400
14011401 if ( templateFrontmatterEnd > 0 ) {
14021402 // Extract and parse template frontmatter
@@ -1435,29 +1435,39 @@ export function analyzeTemplateStructure(templateData: string): {
14351435 result.hasNewNoteTitle = 'newNoteTitle' in result.templateFrontmatter
14361436
14371437 // Check for output frontmatter in the body content
1438+ // Output frontmatter is ONLY valid if the body content STARTS with a separator (--- or --)
14381439 if (result.bodyContent) {
14391440 const bodyLines = result . bodyContent . split ( '\n' )
1440-
1441- // Find separator positions using helper function
1442- const { startIndex : startBlock , endIndex : endBlock } = findSeparatorPositions(bodyLines)
1443-
1444- // Only process as frontmatter if we actually found separator markers
1445- if (startBlock >= 0 && endBlock >= 0 ) {
1446- // Extract and parse output frontmatter
1447- const { attributes , isValid } = extractAndParseFrontmatter(bodyLines, startBlock, endBlock)
1448-
1449- if (isValid) {
1450- result . outputFrontmatter = attributes
1451- result . hasOutputFrontmatter = Object . keys ( result . outputFrontmatter ) . length > 0
1452- result . hasOutputTitle = 'title' in result . outputFrontmatter
1441+ const firstLine = bodyLines [ 0 ] ?. trim ( ) || ''
1442+
1443+ // Only look for output frontmatter if the first line is a separator
1444+ if ( firstLine === '---' || firstLine === '--' ) {
1445+ // Find separator positions using helper function
1446+ const { startIndex : startBlock , endIndex : endBlock } = findSeparatorPositions ( bodyLines )
1447+
1448+ // Only process as frontmatter if we actually found separator markers
1449+ if ( startBlock >= 0 && endBlock >= 0 ) {
1450+ // Extract and parse output frontmatter
1451+ const { attributes , isValid } = extractAndParseFrontmatter ( bodyLines , startBlock , endBlock )
1452+
1453+ if ( isValid ) {
1454+ result . outputFrontmatter = attributes
1455+ result . hasOutputFrontmatter = Object . keys ( result . outputFrontmatter ) . length > 0
1456+ result . hasOutputTitle = 'title' in result . outputFrontmatter
1457+ } else {
1458+ // Not valid frontmatter, so no output frontmatter
1459+ result . outputFrontmatter = { }
1460+ result . hasOutputFrontmatter = false
1461+ result . hasOutputTitle = false
1462+ }
14531463 } else {
1454- // Not valid frontmatter, so no output frontmatter
1464+ // No frontmatter separators found , so no output frontmatter
14551465 result . outputFrontmatter = { }
14561466 result . hasOutputFrontmatter = false
14571467 result . hasOutputTitle = false
14581468 }
14591469 } else {
1460- // No frontmatter separators found , so no output frontmatter
1470+ // Body doesn't start with a separator , so no output frontmatter
14611471 result . outputFrontmatter = { }
14621472 result . hasOutputFrontmatter = false
14631473 result . hasOutputTitle = false
@@ -1732,7 +1742,7 @@ function findInlineTitleAfterFrontmatter(
17321742}
17331743
17341744/**
1735- * Robust helper function to detect inline title in template body content
1745+ * Robust helper function to detect inline title in template body content (after first frontmatter is peeled off)
17361746 * Handles malformed frontmatter, multiple consecutive separators, and multiple frontmatter blocks
17371747 * @param {string } bodyContent - The template body content
17381748 * @returns {{hasInlineTitle: boolean, inlineTitleText: string} }
@@ -1746,9 +1756,24 @@ export function detectInlineTitle(bodyContent: string): { hasInlineTitle: boolea
17461756 const lines = bodyContent . split ( '\n' )
17471757 logDebug ( 'detectInlineTitle' , `Processing ${ lines . length } lines of rendered body content` )
17481758
1749- // Find any note frontmatter blocks in the content
1750- const frontmatterBlocks = findNoteFrontmatterBlock ( lines )
1751- logDebug ( 'detectInlineTitle' , `Found ${ frontmatterBlocks . length } frontmatter blocks` )
1759+ // Check if body content starts with output frontmatter (--- or --)
1760+ // Output frontmatter is ONLY valid if it starts at the beginning of body content
1761+ const firstLine = lines [ 0 ] ?. trim ( ) || ''
1762+ let frontmatterBlocks = [ ]
1763+
1764+ if ( firstLine === '---' || firstLine === '--' ) {
1765+ // Find the output frontmatter block ONLY at the start
1766+ const { startIndex : startBlock , endIndex : endBlock } = findSeparatorPositions ( lines , 0 )
1767+ if ( startBlock === 0 && endBlock >= 0 ) {
1768+ const { isValid } = extractAndParseFrontmatter ( lines , startBlock , endBlock )
1769+ if ( isValid ) {
1770+ frontmatterBlocks = [ { startIndex : startBlock , endIndex : endBlock , isValid : true } ]
1771+ logDebug ( 'detectInlineTitle' , `Found output frontmatter block at start: lines ${ startBlock } -${ endBlock } ` )
1772+ }
1773+ }
1774+ }
1775+
1776+ logDebug ( 'detectInlineTitle' , `Found ${ frontmatterBlocks . length } valid frontmatter blocks` )
17521777
17531778 // Find inline title after processing all frontmatter blocks
17541779 const result = findInlineTitleAfterFrontmatter ( lines , frontmatterBlocks )
0 commit comments