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

View File

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