mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
♻️ Refactor findeTitle()
This commit is contained in:
@@ -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('=')) {
|
|
||||||
title = trimmedLine.substring(0, trimmedLine.length).trim()
|
|
||||||
} else {
|
|
||||||
title = trimmedLine.substring(1, trimmedLine.length).trim()
|
|
||||||
}
|
}
|
||||||
|
if (isInsideCodeBlock === false && (trimmedLine.match(/^# +/) || trimmedNextLine.match(/^=+$/))) {
|
||||||
|
title = trimmedLine
|
||||||
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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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']
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user