Skip to content

Commit dc86600

Browse files
committed
Added preferences to support toggling front matter title extract on/off and to customize what field is used for the title.
1 parent 0e38f61 commit dc86600

File tree

6 files changed

+76
-9
lines changed

6 files changed

+76
-9
lines changed

browser/lib/findNoteTitle.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
export function findNoteTitle (value) {
1+
export function findNoteTitle (value, enableFrontMatterTitle, frontMatterTitleField='title') {
22
const splitted = value.split('\n')
33
let title = null
44
let isInsideCodeBlock = false
55

66
if (splitted[0] === '---') {
77
let line = 0
88
while (++line < splitted.length) {
9-
if (splitted[line].startsWith('title:')) {
10-
title = splitted[line].substring(6).trim()
9+
if (enableFrontMatterTitle && splitted[line].startsWith(frontMatterTitleField + ':')) {
10+
title = splitted[line].substring(frontMatterTitleField.length + 1).trim()
1111

1212
break
1313
}

browser/main/Detail/MarkdownNoteDetail.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class MarkdownNoteDetail extends React.Component {
9191
handleUpdateContent () {
9292
const { note } = this.state
9393
note.content = this.refs.content.value
94-
note.title = markdown.strip(striptags(findNoteTitle(note.content)))
94+
note.title = markdown.strip(striptags(findNoteTitle(note.content, this.props.config.editor.enableFrontMatterTitle, this.props.config.editor.frontMatterTitleField)))
9595
this.updateNote(note)
9696
}
9797

browser/main/Detail/SnippetNoteDetail.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class SnippetNoteDetail extends React.Component {
112112
if (this.refs.tags) note.tags = this.refs.tags.value
113113
note.description = this.refs.description.value
114114
note.updatedAt = new Date()
115-
note.title = findNoteTitle(note.description)
115+
note.title = findNoteTitle(note.description, false)
116116

117117
this.setState({
118118
note

browser/main/lib/ConfigManager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export const DEFAULT_CONFIG = {
4747
scrollPastEnd: false,
4848
type: 'SPLIT',
4949
fetchUrlTitle: true,
50-
enableTableEditor: false
50+
enableTableEditor: false,
51+
enableFrontMatterTitle: true,
52+
frontMatterTitleField: 'title'
5153
},
5254
preview: {
5355
fontSize: '14',

browser/main/modals/PreferencesModal/UiTab.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ class UiTab extends React.Component {
8989
snippetDefaultLanguage: this.refs.editorSnippetDefaultLanguage.value,
9090
scrollPastEnd: this.refs.scrollPastEnd.checked,
9191
fetchUrlTitle: this.refs.editorFetchUrlTitle.checked,
92-
enableTableEditor: this.refs.enableTableEditor.checked
92+
enableTableEditor: this.refs.enableTableEditor.checked,
93+
enableFrontMatterTitle: this.refs.enableFrontMatterTitle.checked,
94+
frontMatterTitleField: this.refs.frontMatterTitleField.value
9395
},
9496
preview: {
9597
fontSize: this.refs.previewFontSize.value,
@@ -428,6 +430,31 @@ class UiTab extends React.Component {
428430
</div>
429431
</div>
430432

433+
<div styleName='group-section'>
434+
<div styleName='group-section-label'>
435+
{i18n.__('Front matter title field')}
436+
</div>
437+
<div styleName='group-section-control'>
438+
<input styleName='group-section-control-input'
439+
ref='frontMatterTitleField'
440+
value={config.editor.frontMatterTitleField}
441+
onChange={(e) => this.handleUIChange(e)}
442+
type='text'
443+
/>
444+
</div>
445+
</div>
446+
447+
<div styleName='group-checkBoxSection'>
448+
<label>
449+
<input onChange={(e) => this.handleUIChange(e)}
450+
checked={this.state.config.editor.enableFrontMatterTitle}
451+
ref='enableFrontMatterTitle'
452+
type='checkbox'
453+
/>&nbsp;
454+
{i18n.__('Extract title from front matter')}
455+
</label>
456+
</div>
457+
431458
<div styleName='group-checkBoxSection'>
432459
<label>
433460
<input onChange={(e) => this.handleUIChange(e)}

tests/lib/find-title-test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,52 @@ test('findNoteTitle#find should return a correct title (string)', t => {
1515
['====', '===='],
1616
['```\n# hoge\n```', '```'],
1717
['hoge', 'hoge'],
18-
['---\nlayout: test\n---\n # hoge', '# hoge'],
18+
['---\nlayout: test\n---\n # hoge', '# hoge']
19+
]
20+
21+
testCases.forEach(testCase => {
22+
const [input, expected] = testCase
23+
t.is(findNoteTitle(input, false), expected, `Test for find() input: ${input} expected: ${expected}`)
24+
})
25+
})
26+
27+
test('findNoteTitle#find should ignore front matter when enableFrontMatterTitle=false', t => {
28+
// [input, expected]
29+
const testCases = [
30+
['---\nlayout: test\ntitle: hoge hoge hoge \n---\n# fuga', '# fuga'],
31+
['---\ntitle:hoge\n---\n# fuga', '# fuga'],
32+
['title: fuga\n# hoge', '# hoge']
33+
]
34+
35+
testCases.forEach(testCase => {
36+
const [input, expected] = testCase
37+
t.is(findNoteTitle(input, false), expected, `Test for find() input: ${input} expected: ${expected}`)
38+
})
39+
})
40+
41+
test('findNoteTitle#find should respect front matter when enableFrontMatterTitle=true', t => {
42+
// [input, expected]
43+
const testCases = [
1944
['---\nlayout: test\ntitle: hoge hoge hoge \n---\n# fuga', 'hoge hoge hoge'],
2045
['---\ntitle:hoge\n---\n# fuga', 'hoge'],
2146
['title: fuga\n# hoge', '# hoge']
2247
]
2348

2449
testCases.forEach(testCase => {
2550
const [input, expected] = testCase
26-
t.is(findNoteTitle(input), expected, `Test for find() input: ${input} expected: ${expected}`)
51+
t.is(findNoteTitle(input, true), expected, `Test for find() input: ${input} expected: ${expected}`)
2752
})
2853
})
54+
55+
test('findNoteTitle#find should respect frontMatterTitleField when provided', t => {
56+
// [input, expected]
57+
const testCases = [
58+
['---\ntitle: hoge\n---\n# fuga', '# fuga'],
59+
['---\ncustom: hoge\n---\n# fuga', 'hoge']
60+
]
61+
62+
testCases.forEach(testCase => {
63+
const [input, expected] = testCase
64+
t.is(findNoteTitle(input, true, 'custom'), expected, `Test for find() input: ${input} expected: ${expected}`)
65+
})
66+
})

0 commit comments

Comments
 (0)