1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-12 17:26:17 +00:00

Title will now be extracted from front matter if title: is present.

This commit is contained in:
David Nagle
2018-10-19 10:59:37 -07:00
parent ff3026686f
commit 0e38f61c85
2 changed files with 22 additions and 13 deletions

View File

@@ -6,6 +6,11 @@ export function findNoteTitle (value) {
if (splitted[0] === '---') {
let line = 0
while (++line < splitted.length) {
if (splitted[line].startsWith('title:')) {
title = splitted[line].substring(6).trim()
break
}
if (splitted[line] === '---') {
splitted.splice(0, line + 1)
@@ -14,17 +19,19 @@ export function findNoteTitle (value) {
}
}
splitted.some((line, index) => {
const trimmedLine = line.trim()
const trimmedNextLine = splitted[index + 1] === undefined ? '' : splitted[index + 1].trim()
if (trimmedLine.match('```')) {
isInsideCodeBlock = !isInsideCodeBlock
}
if (isInsideCodeBlock === false && (trimmedLine.match(/^# +/) || trimmedNextLine.match(/^=+$/))) {
title = trimmedLine
return true
}
})
if (title === null) {
splitted.some((line, index) => {
const trimmedLine = line.trim()
const trimmedNextLine = splitted[index + 1] === undefined ? '' : splitted[index + 1].trim()
if (trimmedLine.match('```')) {
isInsideCodeBlock = !isInsideCodeBlock
}
if (isInsideCodeBlock === false && (trimmedLine.match(/^# +/) || trimmedNextLine.match(/^=+$/))) {
title = trimmedLine
return true
}
})
}
if (title === null) {
title = ''

View File

@@ -15,7 +15,10 @@ test('findNoteTitle#find should return a correct title (string)', t => {
['====', '===='],
['```\n# hoge\n```', '```'],
['hoge', 'hoge'],
['---\nlayout: test\n---\n # hoge', '# hoge']
['---\nlayout: test\n---\n # hoge', '# hoge'],
['---\nlayout: test\ntitle: hoge hoge hoge \n---\n# fuga', 'hoge hoge hoge'],
['---\ntitle:hoge\n---\n# fuga', 'hoge'],
['title: fuga\n# hoge', '# hoge']
]
testCases.forEach(testCase => {
@@ -23,4 +26,3 @@ test('findNoteTitle#find should return a correct title (string)', t => {
t.is(findNoteTitle(input), expected, `Test for find() input: ${input} expected: ${expected}`)
})
})