diff --git a/browser/lib/findNoteTitle.js b/browser/lib/findNoteTitle.js index b954f172..f28e44de 100644 --- a/browser/lib/findNoteTitle.js +++ b/browser/lib/findNoteTitle.js @@ -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 = '' diff --git a/tests/lib/find-title-test.js b/tests/lib/find-title-test.js index f587804c..1e9daa0f 100644 --- a/tests/lib/find-title-test.js +++ b/tests/lib/find-title-test.js @@ -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}`) }) }) -