Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions browser/lib/findNoteTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ export function findNoteTitle (value) {
let title = null
let isInsideCodeBlock = false

if (splitted[0] === '---') {
let index = 0
while (++index < splitted.length && 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()
Expand Down
21 changes: 21 additions & 0 deletions browser/lib/markdown-it-frontmatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

module.exports = function frontMatterPlugin (md) {
function frontmatter (state, startLine, endLine, silent) {
if (startLine !== 0 || state.src.substr(startLine, state.eMarks[0]) !== '---') {
return false
}

let line = 0
while (++line < state.lineMax && state.src.substring(state.bMarks[line], state.eMarks[line]) !== '---') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we should display the markdown if the front-matter syntax is invalid. For example:

---
layout: default
-----
sdasdasdasdasd

Should display:
image
With the current behavior (hide all content) the user will get confuse.

Here is my suggestion for the plugin:

module.exports = function frontMatterPlugin (md) {
  function frontmatter (state, startLine, endLine, silent) {
    if (startLine !== 0 || state.src.substr(startLine, state.eMarks[0]) !== '---') {
      return false
    }

    let line = 0
    let isFrontMatterClosed = false
    while (line < state.lineMax) {
      line++
      if (state.src.substring(state.bMarks[line], state.eMarks[line]) === '---') {
        isFrontMatterClosed = true
        break
      }
    }

    state.line = line + 1

    return isFrontMatterClosed
  }

  md.block.ruler.before('table', 'frontmatter', frontmatter, {
    alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
  })
}

The the modification above, the front-matter will be displayed out if it's invalid and will be hide only when it's valid.

}

state.line = line + 1

return true
}

md.block.ruler.before('table', 'frontmatter', frontmatter, {
alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
})
}
1 change: 1 addition & 0 deletions browser/lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class Markdown {
})
this.md.use(require('markdown-it-kbd'))
this.md.use(require('markdown-it-admonition'))
this.md.use(require('./markdown-it-frontmatter'))

const deflate = require('markdown-it-plantuml/lib/deflate')
this.md.use(require('markdown-it-plantuml'), '', {
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/find-title-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ test('findNoteTitle#find should return a correct title (string)', t => {
['hoge\n====\nfuga', 'hoge'],
['====', '===='],
['```\n# hoge\n```', '```'],
['hoge', 'hoge']
['hoge', 'hoge'],
['---\nlayout: test\n---\n # hoge', '# hoge']
]

testCases.forEach(testCase => {
Expand Down