Skip to content

Commit 0526f0d

Browse files
committed
import embed from frontmatter parser
1 parent 6438443 commit 0526f0d

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/utils/frontmatter/embed.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { doc, util } from 'prettier';
2+
// import inferParser from '../../utilities/infer-parser.js';
3+
import isFrontMatter from './is-front-matter.js';
4+
5+
const SUPPORTED_EMBED_LANGUAGES = new Set(['yaml', 'toml']);
6+
7+
const hardline = doc.builders.hardline;
8+
const markAsRoot = doc.builders.markAsRoot;
9+
const inferParser = util.inferParser;
10+
11+
const isEmbedFrontMatter = ({ node } /* , options */) =>
12+
isFrontMatter(node) && SUPPORTED_EMBED_LANGUAGES.has(node.language);
13+
14+
async function printEmbedFrontMatter(textToDoc, print, path, options) {
15+
const { node } = path;
16+
const { language } = node;
17+
18+
if (!SUPPORTED_EMBED_LANGUAGES.has(language)) {
19+
return;
20+
}
21+
22+
const value = node.value.trim();
23+
24+
let doc;
25+
if (value) {
26+
const parser =
27+
language === 'yaml' ? language : inferParser(options, { language });
28+
if (!parser) {
29+
return;
30+
}
31+
32+
doc = value ? await textToDoc(value, { parser }) : '';
33+
} else {
34+
doc = value;
35+
}
36+
37+
return markAsRoot([
38+
node.startDelimiter,
39+
node.explicitLanguage ?? '',
40+
hardline,
41+
doc,
42+
doc ? hardline : '',
43+
node.endDelimiter,
44+
]);
45+
}
46+
47+
export { isEmbedFrontMatter, printEmbedFrontMatter };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { FRONT_MATTER_MARK } from './constants.js';
2+
3+
function isFrontMatter(node) {
4+
return Boolean(node?.[FRONT_MATTER_MARK]);
5+
}
6+
7+
export default isFrontMatter;

0 commit comments

Comments
 (0)