Skip to content

Commit

Permalink
fix(parser): note detection
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 26, 2021
1 parent 1c47aad commit e7d33d8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
16 changes: 10 additions & 6 deletions packages/parser/src/core.ts
Expand Up @@ -68,12 +68,16 @@ export function parseSlide(raw: string): SlideInfoBase {
const result = matter(raw)
let note: string | undefined
const frontmatter = result.data || {}
const content = result.content
.trim()
.replace(/<!--([\s\S]*)-->$/g, (_, v = '') => {
note = v.trim()
return ''
})
let content = result.content.trim()

const comments = Array.from(content.matchAll(/<!--([\s\S]*?)-->/g))
if (comments.length) {
const last = comments[comments.length - 1]
if (last.index && last.index + last[0].length >= content.length) {
note = last[1].trim()
content = content.slice(0, last.index).trim()
}
}

const title = frontmatter.title || frontmatter.name || content.match(/^#+ (.*)$/m)?.[1]?.trim()

Expand Down
19 changes: 11 additions & 8 deletions test/__snapshots__/parser.test.ts.snap
Expand Up @@ -136,7 +136,7 @@ title: Hi
},
},
"index": 1,
"note": "This is notes",
"note": "This is note",
"raw": "---
meta:
title: FooBar
Expand All @@ -147,7 +147,7 @@ layout: center
# Hello
<!--
This is notes
This is note
-->
",
"start": 8,
Expand All @@ -170,22 +170,25 @@ This is notes
},
Object {
"content": "
<!-- This is not notes -->
<!-- This is not note -->
Hey
",
"end": 24,
"end": 26,
"frontmatter": Object {
"layout": "text",
},
"index": 3,
"note": undefined,
"note": "This is note",
"raw": "---
layout: text
---
<!-- This is not notes -->
<!-- This is not note -->
Hey
<!--
This is note
-->
",
"start": 19,
"title": undefined,
Expand All @@ -199,7 +202,7 @@ this should be treated as code block
----
\`\`\`
",
"end": 33,
"end": 35,
"frontmatter": Object {},
"index": 4,
"note": undefined,
Expand All @@ -212,7 +215,7 @@ this should be treated as code block
\`\`\`
",
"start": 25,
"start": 27,
"title": undefined,
},
]
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/markdown/frontmatter.md
Expand Up @@ -13,15 +13,17 @@ meta:
layout: center
---
# Hello
<!-- This is notes -->
<!-- This is note -->
---

# Morning
---
layout: text
---
<!-- This is not notes -->
<!-- This is not note -->
Hey
<!-- This is note -->

---

```md
Expand Down

0 comments on commit e7d33d8

Please sign in to comment.