-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathembed.js
More file actions
47 lines (37 loc) · 1.13 KB
/
embed.js
File metadata and controls
47 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { doc, util } from 'prettier';
// import inferParser from '../../utilities/infer-parser.js';
import isFrontMatter from './is-front-matter.js';
const SUPPORTED_EMBED_LANGUAGES = new Set(['yaml', 'toml']);
const hardline = doc.builders.hardline;
const markAsRoot = doc.builders.markAsRoot;
const inferParser = util.inferParser;
const isEmbedFrontMatter = ({ node } /* , options */) =>
isFrontMatter(node) && SUPPORTED_EMBED_LANGUAGES.has(node.language);
async function printEmbedFrontMatter(textToDoc, print, path, options) {
const { node } = path;
const { language } = node;
if (!SUPPORTED_EMBED_LANGUAGES.has(language)) {
return;
}
const value = node.value.trim();
let doc;
if (value) {
const parser =
language === 'yaml' ? language : inferParser(options, { language });
if (!parser) {
return;
}
doc = value ? await textToDoc(value, { parser }) : '';
} else {
doc = value;
}
return markAsRoot([
node.startDelimiter,
node.explicitLanguage ?? '',
hardline,
doc,
doc ? hardline : '',
node.endDelimiter,
]);
}
export { isEmbedFrontMatter, printEmbedFrontMatter };