Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/format/markdown/format-markdown.ts
6451 views
1
/*
2
* format-markdown.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { kFrom, kOutputDivs, kToc, kVariant } from "../../config/constants.ts";
8
import { Format } from "../../config/types.ts";
9
import { pandocFormat } from "../../core/pandoc/pandoc-formats.ts";
10
import { createFormat, plaintextFormat } from "../formats-shared.ts";
11
import { kGfmCommonmarkVariant } from "./format-markdown-consts.ts";
12
13
export function requiresShortcodeUnescapePostprocessor(markdown: string) {
14
return markdown.includes("{{{<");
15
}
16
17
const kDisplayNameGFM = "Github (GFM)";
18
const kDisplayNameCommonMark = "CommonMark";
19
const kDisplayNameMarkdown = "Markdown";
20
21
export function shortcodeUnescapePostprocessor(output: string): Promise<void> {
22
// unescape shortcodes
23
Deno.writeTextFileSync(
24
output,
25
Deno.readTextFileSync(output)
26
.replaceAll("{{\\<", "{{<")
27
.replaceAll("\\>}}", ">}}"),
28
);
29
return Promise.resolve();
30
}
31
32
export function gfmFormat(): Format {
33
return createFormat(kDisplayNameGFM, "md", markdownFormat(kDisplayNameGFM), {
34
pandoc: {
35
to: "commonmark",
36
},
37
render: {
38
[kVariant]: kGfmCommonmarkVariant,
39
},
40
resolveFormat: (format: Format) => {
41
if (format.pandoc[kToc] === true) {
42
// we need to add gfm_auto_identifiers to reader otherwise ids for toc are not valid
43
// see https://github.com/quarto-dev/quarto-cli/issues/4917 and
44
// https://github.com/jgm/pandoc/issues/8709
45
format.pandoc[kFrom] = pandocFormat(
46
format.pandoc[kFrom] || "markdown",
47
["gfm_auto_identifiers"],
48
[],
49
);
50
}
51
},
52
});
53
}
54
55
// for 'md' alias
56
export function markdownWithCommonmarkExtensionsFormat() {
57
return createFormat(
58
kDisplayNameCommonMark,
59
"md",
60
markdownFormat(kDisplayNameCommonMark),
61
{
62
pandoc: {
63
to: [
64
"markdown_strict",
65
"+raw_html",
66
"+all_symbols_escapable",
67
"+backtick_code_blocks",
68
"+fenced_code_blocks",
69
"+space_in_atx_header",
70
"+intraword_underscores",
71
"+lists_without_preceding_blankline",
72
"+shortcut_reference_links",
73
].join(""),
74
},
75
},
76
);
77
}
78
79
export function commonmarkFormat(to: string) {
80
return createFormat(
81
kDisplayNameCommonMark,
82
"md",
83
markdownFormat(kDisplayNameCommonMark),
84
{
85
pandoc: {
86
to,
87
},
88
},
89
);
90
}
91
92
export function pandocMarkdownFormat(): Format {
93
return createFormat(
94
kDisplayNameMarkdown,
95
"md",
96
plaintextFormat(kDisplayNameMarkdown, "md"),
97
{},
98
);
99
}
100
101
export function markdownFormat(displayName: string): Format {
102
return createFormat(displayName, "md", plaintextFormat(displayName, "md"), {
103
// markdown shouldn't include cell divs (even if it
104
// technically supports raw html)
105
render: {
106
[kOutputDivs]: false,
107
},
108
});
109
}
110
111