1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 01:36:22 +00:00
Files
Boostnote/browser/lib/findNoteTitle.js
Baptiste Augrain 20f573c477 - hide front-matter in preview
- skip front-matter when looking for note's title
2018-08-27 10:06:20 +02:00

44 lines
995 B
JavaScript

const frontMatterRegex = /^\-{3,}/
export function findNoteTitle (value) {
const splitted = value.split('\n')
let title = null
let isInsideCodeBlock = false
if (frontMatterRegex.exec(splitted[0])) {
let index = 0
while (++index < splitted.length && !frontMatterRegex.exec(splitted[index])) {
}
splitted.splice(0, index + 1)
}
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 = ''
splitted.some((line) => {
if (line.trim().length > 0) {
title = line.trim()
return true
}
})
}
return title
}
export default {
findNoteTitle
}