Skip to content

Commit 22030f4

Browse files
authored
Merge pull request #1148 from sytone/develop
Updated embed to strip YAML front matter fixes #1129
2 parents fd5ed35 + b05949a commit 22030f4

File tree

7 files changed

+70
-1
lines changed

7 files changed

+70
-1
lines changed

docs/_media/example-with-yaml.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
author: John Smith
3+
date: 2020-1-1
4+
---
5+
6+
> This is from the `example.md`

docs/embed-files.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,20 @@ You will get it
3939

4040
[filename](_media/example.md ':include :type=code')
4141

42+
## Markdown with YAML Front Matter
43+
44+
When using Markdown, YAML front matter will be stripped from the rendered content. The attributes cannot be used in this case.
45+
46+
```markdown
47+
[filename](_media/example-with-yaml.md ':include')
48+
```
49+
50+
You will get just the content
51+
52+
[filename](_media/example-with-yaml.md ':include')
53+
4254
## Embedded code fragments
55+
4356
Sometimes you don't want to embed a whole file. Maybe because you need just a few lines but you want to compile and test the file in CI.
4457

4558
```markdown
@@ -53,7 +66,6 @@ Example:
5366

5467
[filename](_media/example.js ':include :type=code :fragment=demo')
5568

56-
5769
## Tag attribute
5870

5971
If you embed the file as `iframe`, `audio` and `video`, then you may need to set the attributes of these tags.

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
</script>
7777
<script src="/lib/docsify.js"></script>
7878
<script src="/lib/plugins/search.js"></script>
79+
<script src="/lib/plugins/front-matter.js"></script>
7980
<script src="//unpkg.com/prismjs/components/prism-bash.min.js"></script>
8081
<script src="//unpkg.com/prismjs/components/prism-markdown.min.js"></script>
8182
<script src="//unpkg.com/prismjs/components/prism-nginx.min.js"></script>

src/core/render/compiler.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ export class Compiler {
121121
};
122122
}
123123

124+
/**
125+
* Pulls content from file and renders inline on the page as a embedded item.
126+
*
127+
* This allows you to embed different file types on the returned
128+
* page.
129+
* The basic format is:
130+
* ```
131+
* [filename](_media/example.md ':include')
132+
* ```
133+
*
134+
* @param {string} href The href to the file to embed in the page.
135+
* @param {string} title Title of the link used to make the embed.
136+
*
137+
* @return {type} Return value description.
138+
*/
124139
compileEmbed(href, title) {
125140
const { str, config } = getAndRemoveConfig(title);
126141
let embed;

src/core/render/embed.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
3636
return x;
3737
});
3838

39+
// This may contain YAML front matter and will need to be stripped.
40+
const frontMatterInstalled =
41+
($docsify.frontMatter || {}).installed || false;
42+
if (frontMatterInstalled === true) {
43+
text = $docsify.frontMatter.parseMarkdown(text);
44+
}
45+
3946
embedToken = compile.lexer(text);
4047
} else if (token.embed.type === 'code') {
4148
if (token.embed.fragment) {

src/core/render/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/**
2+
* Converts a colon formatted string to a object with properties.
3+
*
4+
* This is process a provided string and look for any tokens in the format
5+
* of `:name[=value]` and then convert it to a object and return.
6+
* An example of this is ':include :type=code :fragment=demo' is taken and
7+
* then converted to:
8+
*
9+
* ```
10+
* {
11+
* include: '',
12+
* type: 'code',
13+
* fragment: 'demo'
14+
* }
15+
* ```
16+
*
17+
* @param {string} str The string to parse.
18+
*
19+
* @return {object} The original string and parsed object, { str, config }.
20+
*/
121
export function getAndRemoveConfig(str = '') {
222
const config = {};
323

src/plugins/front-matter/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import parser from './parser';
22

33
const install = function(hook, vm) {
4+
// Used to remove front matter from embedded pages if installed.
5+
vm.config.frontMatter = {};
6+
vm.config.frontMatter.installed = true;
7+
vm.config.frontMatter.parseMarkdown = function(content) {
8+
const { body } = parser(content);
9+
return body;
10+
};
11+
412
hook.beforeEach(content => {
513
const { attributes, body } = parser(content);
614

0 commit comments

Comments
 (0)