mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-14 02:06:29 +00:00
Merge pull request #548 from asmsuechan/add-a-module-findTitle
Add a module to find the title
This commit is contained in:
33
browser/lib/findNoteTitle.js
Normal file
33
browser/lib/findNoteTitle.js
Normal 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
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ import ee from 'browser/main/lib/eventEmitter'
|
|||||||
import markdown from 'browser/lib/markdown'
|
import markdown from 'browser/lib/markdown'
|
||||||
import StatusBar from '../StatusBar'
|
import StatusBar from '../StatusBar'
|
||||||
import _ from 'lodash'
|
import _ from 'lodash'
|
||||||
|
import { findNoteTitle } from 'browser/lib/findNoteTitle'
|
||||||
|
|
||||||
const electron = require('electron')
|
const electron = require('electron')
|
||||||
const { remote } = electron
|
const { remote } = electron
|
||||||
@@ -63,44 +64,6 @@ class MarkdownNoteDetail extends React.Component {
|
|||||||
ee.off('topbar:togglelockbutton', this.toggleLockButton)
|
ee.off('topbar:togglelockbutton', this.toggleLockButton)
|
||||||
}
|
}
|
||||||
|
|
||||||
findTitle (value) {
|
|
||||||
let splitted = value.split('\n')
|
|
||||||
let title = null
|
|
||||||
let isMarkdownInCode = 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()
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (title == null) {
|
|
||||||
for (let i = 0; i < splitted.length; i++) {
|
|
||||||
let trimmedLine = splitted[i].trim()
|
|
||||||
if (trimmedLine.length > 0) {
|
|
||||||
title = trimmedLine
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (title == null) {
|
|
||||||
title = ''
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
title = markdown.strip(title)
|
|
||||||
|
|
||||||
return title
|
|
||||||
}
|
|
||||||
|
|
||||||
getPercentageOfCompleteTodo (noteContent) {
|
getPercentageOfCompleteTodo (noteContent) {
|
||||||
let splitted = noteContent.split('\n')
|
let splitted = noteContent.split('\n')
|
||||||
let numberOfTodo = 0
|
let numberOfTodo = 0
|
||||||
@@ -124,7 +87,7 @@ class MarkdownNoteDetail extends React.Component {
|
|||||||
|
|
||||||
note.content = this.refs.content.value
|
note.content = this.refs.content.value
|
||||||
note.tags = this.refs.tags.value
|
note.tags = this.refs.tags.value
|
||||||
note.title = this.findTitle(note.content)
|
note.title = markdown.strip(findNoteTitle(note.content))
|
||||||
note.updatedAt = new Date()
|
note.updatedAt = new Date()
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import StatusBar from '../StatusBar'
|
|||||||
import context from 'browser/lib/context'
|
import context from 'browser/lib/context'
|
||||||
import ConfigManager from 'browser/main/lib/ConfigManager'
|
import ConfigManager from 'browser/main/lib/ConfigManager'
|
||||||
import _ from 'lodash'
|
import _ from 'lodash'
|
||||||
|
import { findNoteTitle } from 'browser/lib/findNoteTitle'
|
||||||
|
|
||||||
function pass (name) {
|
function pass (name) {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
@@ -75,41 +76,13 @@ class SnippetNoteDetail extends React.Component {
|
|||||||
if (this.saveQueue != null) this.saveNow()
|
if (this.saveQueue != null) this.saveNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
findTitle (value) {
|
|
||||||
let splitted = value.split('\n')
|
|
||||||
let title = null
|
|
||||||
|
|
||||||
for (let i = 0; i < splitted.length; i++) {
|
|
||||||
let trimmedLine = splitted[i].trim()
|
|
||||||
if (trimmedLine.match(/^# .+/)) {
|
|
||||||
title = trimmedLine.substring(1, trimmedLine.length).trim()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (title == null) {
|
|
||||||
for (let i = 0; i < splitted.length; i++) {
|
|
||||||
let trimmedLine = splitted[i].trim()
|
|
||||||
if (trimmedLine.length > 0) {
|
|
||||||
title = trimmedLine
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (title == null) {
|
|
||||||
title = ''
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return title
|
|
||||||
}
|
|
||||||
|
|
||||||
handleChange (e) {
|
handleChange (e) {
|
||||||
let { note } = this.state
|
let { note } = this.state
|
||||||
|
|
||||||
note.tags = this.refs.tags.value
|
note.tags = this.refs.tags.value
|
||||||
note.description = this.refs.description.value
|
note.description = this.refs.description.value
|
||||||
note.updatedAt = new Date()
|
note.updatedAt = new Date()
|
||||||
note.title = this.findTitle(note.description)
|
note.title = findNoteTitle(note.description)
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
note
|
note
|
||||||
|
|||||||
25
tests/lib/find-title-test.js
Normal file
25
tests/lib/find-title-test.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* @fileoverview Unit test for browser/lib/findTitle
|
||||||
|
*/
|
||||||
|
|
||||||
|
const test = require('ava')
|
||||||
|
const { findNoteTitle } = require('browser/lib/findNoteTitle')
|
||||||
|
|
||||||
|
// Unit test
|
||||||
|
test('findNoteTitle#find should return a correct title (string)', t => {
|
||||||
|
// [input, expected]
|
||||||
|
const testCases = [
|
||||||
|
['# hoge\nfuga', '# hoge'],
|
||||||
|
['# hoge_hoge_hoge', '# hoge_hoge_hoge'],
|
||||||
|
['hoge\n====\nfuga', 'hoge'],
|
||||||
|
['====', '===='],
|
||||||
|
['```\n# hoge\n```', '```'],
|
||||||
|
['hoge', 'hoge']
|
||||||
|
]
|
||||||
|
|
||||||
|
testCases.forEach(testCase => {
|
||||||
|
const [input, expected] = testCase
|
||||||
|
t.is(findNoteTitle(input), expected, `Test for find() input: ${input} expected: ${expected}`)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
Reference in New Issue
Block a user