Skip to content

Commit e6ab47c

Browse files
authored
fix(embed): strip front matter from included markdown (docsifyjs#2752)
1 parent 018ff1c commit e6ab47c

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

docs/embed-files.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ Front Matter, commonly utilized in blogging systems like Jekyll, serves to defin
4848

4949
When using Markdown, YAML front matter will be stripped from the rendered content. The attributes cannot be used in this case.
5050

51+
```html
52+
<script src="//cdn.jsdelivr.net/npm/docsify@5/dist/plugins/front-matter.min.js"></script>
53+
```
54+
5155
```markdown
5256
[filename](_media/example-with-yaml.md ':include')
5357
```

src/core/render/embed.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function extractFragmentContent(text, fragment, fullLine) {
3232
return stripIndent((match || [])[1] || '').trim();
3333
}
3434

35-
function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
35+
function walkFetchEmbed({ embedTokens, compile, fetch, frontMatter }, cb) {
3636
if (!embedTokens.length) {
3737
return cb({});
3838
}
@@ -62,9 +62,9 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
6262
});
6363

6464
// This may contain YAML front matter and will need to be stripped.
65-
const frontMatterInstalled = $docsify?.frontMatter?.installed;
65+
const frontMatterInstalled = frontMatter?.installed;
6666
if (frontMatterInstalled) {
67-
text = $docsify.frontMatter?.parseMarkdown(text);
67+
text = frontMatter?.parseMarkdown(text);
6868
}
6969

7070
if (currentToken.embed.fragment) {
@@ -140,6 +140,7 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
140140
}
141141

142142
const compile = compiler._marked;
143+
const frontMatter = compiler.config.frontMatter;
143144
let tokens = compile.lexer(raw);
144145
const embedTokens = [];
145146
const links = tokens.links;
@@ -203,7 +204,7 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
203204
const moves = [];
204205
const tokenInsertState = new WeakMap();
205206
walkFetchEmbed(
206-
{ compile, embedTokens, fetch },
207+
{ compile, embedTokens, fetch, frontMatter },
207208
({ embedToken, token, rowIndex, cellIndex, tokenRef }) => {
208209
if (token && embedToken) {
209210
Object.assign(links, embedToken.links);

test/integration/embed.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,40 @@ describe('Embed', function () {
179179
expect(mainText).not.toContain("_media/second.md ':include'");
180180
});
181181

182+
test('embed markdown file strips front matter when plugin is installed', async () => {
183+
await docsifyInit({
184+
markdown: {
185+
homepage: `
186+
---
187+
title: Homepage
188+
---
189+
190+
# Embed Test
191+
192+
[front matter include](_media/content.md ':include')
193+
`,
194+
},
195+
routes: {
196+
'_media/content.md': `
197+
---
198+
title: Include
199+
---
200+
201+
included front matter content
202+
`,
203+
},
204+
scriptURLs: ['/dist/plugins/front-matter.js'],
205+
});
206+
207+
expect(
208+
await waitForText('#main', 'included front matter content'),
209+
).toBeTruthy();
210+
211+
const mainText = document.querySelector('#main').textContent;
212+
expect(mainText).not.toContain('title: Homepage');
213+
expect(mainText).not.toContain('title: Include');
214+
});
215+
182216
test('embed multiple include code fragments in same paragraph', async () => {
183217
await docsifyInit({
184218
markdown: {

0 commit comments

Comments
 (0)