1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 01:36:22 +00:00

♻️ Refactor findeTitle()

This commit is contained in:
asmsuechan
2017-05-23 16:51:58 +09:00
parent 0f71139eba
commit 99228f2e60
2 changed files with 13 additions and 16 deletions

View File

@@ -1,29 +1,25 @@
export function findNoteTitle (value) { export function findNoteTitle (value) {
let splitted = value.split('\n') let splitted = value.split('\n')
let title = null let title = null
let isMarkdownInCode = false let isInsideCodeBlock = false
splitted.some((line, index) => { splitted.some((line, index) => {
let trimmedLine = line.trim() let trimmedLine = line.trim()
let trimmedNextLine = splitted[index + 1] === undefined ? '' : splitted[index + 1].trim() let trimmedNextLine = splitted[index + 1] === undefined ? '' : splitted[index + 1].trim()
if (trimmedLine.match('```')) { if (trimmedLine.match('```')) {
isMarkdownInCode = !isMarkdownInCode isInsideCodeBlock = !isInsideCodeBlock
} else if (isMarkdownInCode === false && (trimmedLine.match(/^# +/) || trimmedNextLine.match('='))) { }
if (trimmedNextLine.match('=')) { if (isInsideCodeBlock === false && (trimmedLine.match(/^# +/) || trimmedNextLine.match(/^=+$/))) {
title = trimmedLine.substring(0, trimmedLine.length).trim() title = trimmedLine
} else {
title = trimmedLine.substring(1, trimmedLine.length).trim()
}
return true return true
} }
}) })
if (title == null) { if (title === null) {
title = '' title = ''
splitted.some((line, index) => { splitted.some((line) => {
let trimmedLine = splitted[index].trim() if (line.trim().length > 0) {
if (trimmedLine.length > 0) { title = line.trim()
title = trimmedLine
return true return true
} }
}) })

View File

@@ -9,9 +9,10 @@ const { findNoteTitle } = require('browser/lib/findNoteTitle')
test('findNoteTitle#find should return a correct title (string)', t => { test('findNoteTitle#find should return a correct title (string)', t => {
// [input, expected] // [input, expected]
const testCases = [ const testCases = [
['# hoge\nfuga', 'hoge'], ['# hoge\nfuga', '# hoge'],
['# hoge_hoge_hoge', 'hoge_hoge_hoge'], ['# hoge_hoge_hoge', '# hoge_hoge_hoge'],
['```\n# hoge\n```\n# fuga', 'fuga'], ['hoge\n====\nfuga', 'hoge'],
['====', '===='],
['```\n# hoge\n```', '```'], ['```\n# hoge\n```', '```'],
['hoge', 'hoge'] ['hoge', 'hoge']
] ]