Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions docs/embed-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Front Matter, commonly utilized in blogging systems like Jekyll, serves to defin

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

```html
<script src="//cdn.jsdelivr.net/npm/docsify@5/dist/plugins/front-matter.min.js"></script>
```

```markdown
[filename](_media/example-with-yaml.md ':include')
```
Expand Down
9 changes: 5 additions & 4 deletions src/core/render/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function extractFragmentContent(text, fragment, fullLine) {
return stripIndent((match || [])[1] || '').trim();
}

function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
function walkFetchEmbed({ embedTokens, compile, fetch, frontMatter }, cb) {
if (!embedTokens.length) {
return cb({});
}
Expand Down Expand Up @@ -62,9 +62,9 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
});

// This may contain YAML front matter and will need to be stripped.
const frontMatterInstalled = $docsify?.frontMatter?.installed;
const frontMatterInstalled = frontMatter?.installed;
if (frontMatterInstalled) {
text = $docsify.frontMatter?.parseMarkdown(text);
text = frontMatter?.parseMarkdown(text);
}

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

const compile = compiler._marked;
const frontMatter = compiler.config.frontMatter;
let tokens = compile.lexer(raw);
const embedTokens = [];
const links = tokens.links;
Expand Down Expand Up @@ -203,7 +204,7 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
const moves = [];
const tokenInsertState = new WeakMap();
walkFetchEmbed(
{ compile, embedTokens, fetch },
{ compile, embedTokens, fetch, frontMatter },
({ embedToken, token, rowIndex, cellIndex, tokenRef }) => {
if (token && embedToken) {
Object.assign(links, embedToken.links);
Expand Down
34 changes: 34 additions & 0 deletions test/integration/embed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,40 @@ describe('Embed', function () {
expect(mainText).not.toContain("_media/second.md ':include'");
});

test('embed markdown file strips front matter when plugin is installed', async () => {
await docsifyInit({
markdown: {
homepage: `
---
title: Homepage
---

# Embed Test

[front matter include](_media/content.md ':include')
`,
},
routes: {
'_media/content.md': `
---
title: Include
---

included front matter content
`,
},
scriptURLs: ['/dist/plugins/front-matter.js'],
});

expect(
await waitForText('#main', 'included front matter content'),
).toBeTruthy();

const mainText = document.querySelector('#main').textContent;
expect(mainText).not.toContain('title: Homepage');
expect(mainText).not.toContain('title: Include');
});

test('embed multiple include code fragments in same paragraph', async () => {
await docsifyInit({
markdown: {
Expand Down
Loading