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

Merge pull request #548 from asmsuechan/add-a-module-findTitle

Add a module to find the title
This commit is contained in:
SuenagaRyota
2017-06-07 10:30:46 +09:00
committed by GitHub
4 changed files with 62 additions and 68 deletions

View File

@@ -0,0 +1,33 @@
export function findNoteTitle (value) {
let splitted = value.split('\n')
let title = null
let isInsideCodeBlock = false
splitted.some((line, index) => {
let trimmedLine = line.trim()
let 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 = ''
splitted.some((line) => {
if (line.trim().length > 0) {
title = line.trim()
return true
}
})
}
return title
}
export default {
findNoteTitle
}