mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-06-05 01:04:25 -04:00
fix: frontmatter headline
If one wrote a frontmatter the incomplete ending dashes where interpreted as a headline and therefore the last line in the frontmatter was handled as the first heading of the document. Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
61fc33fc73
commit
02a5f62d27
6 changed files with 38 additions and 3 deletions
|
@ -96,4 +96,17 @@ describe('frontmatter extraction', () => {
|
|||
expect(extraction?.rawText).toEqual('multi\nline')
|
||||
})
|
||||
})
|
||||
|
||||
describe('is incomplete', () => {
|
||||
it('if frontmatter is closed with one dash', () => {
|
||||
const testNote = ['---', 'type: document', '-', 'content']
|
||||
const extraction = extractFrontmatter(testNote)
|
||||
expect(extraction?.incomplete).toBeTruthy()
|
||||
})
|
||||
it('if frontmatter is closed with two dash', () => {
|
||||
const testNote = ['---', 'type: document', '-', 'content']
|
||||
const extraction = extractFrontmatter(testNote)
|
||||
expect(extraction?.incomplete).toBeTruthy()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -7,6 +7,7 @@ import type { FrontmatterExtractionResult } from './types.js'
|
|||
|
||||
const FRONTMATTER_BEGIN_REGEX = /^-{3,}$/
|
||||
const FRONTMATTER_END_REGEX = /^(?:-{3,}|\.{3,})$/
|
||||
const FRONTMATTER_INCOMPLETE_END_REGEX = /^-{1,2}$/
|
||||
|
||||
/**
|
||||
* Extracts a frontmatter block from a given multiline string.
|
||||
|
@ -25,13 +26,21 @@ export const extractFrontmatter = (
|
|||
return undefined
|
||||
}
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
if (FRONTMATTER_INCOMPLETE_END_REGEX.test(lines[i])) {
|
||||
return {
|
||||
rawText: '',
|
||||
lineOffset: i + 1,
|
||||
incomplete: true
|
||||
}
|
||||
}
|
||||
if (
|
||||
lines[i].length === lines[0].length &&
|
||||
FRONTMATTER_END_REGEX.test(lines[i])
|
||||
) {
|
||||
return {
|
||||
rawText: lines.slice(1, i).join('\n'),
|
||||
lineOffset: i + 1
|
||||
lineOffset: i + 1,
|
||||
incomplete: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,4 +7,5 @@
|
|||
export interface FrontmatterExtractionResult {
|
||||
rawText: string
|
||||
lineOffset: number
|
||||
incomplete: boolean
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue