Fri, 24 Jan 2025 13:27:45 +0100
Fix directory creation, as lfs doesn't do all leaves like the old approach.
21
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | #!/usr/bin/env node |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | /*eslint no-console:0*/ |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | var fs = require('fs'); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | var hljs = require('highlight.js'); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | |
28
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
7 | var katexOptions = { |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
8 | throwOnError: false, |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
9 | macros: { |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
10 | "\\iprod": "{\\langle #1, #2\\rangle}", |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
11 | "\\grad": "\\nabla", |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
12 | "\\isect": "\\cap", |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
13 | "\\union": "\\cup", |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
14 | "\\Isect": "\\bigcap", |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
15 | "\\Union": "\\bigcup", |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
16 | "\\supp": "\\mathop{\\mathrm{supp}}", |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
17 | "\\sign": "\\mathop{\\mathrm{sign}}", |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
18 | "\\Id": "\\mathop{\\mathrm{Id}}", |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
19 | "\\linear": "\\mathbb{L}" |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
20 | } |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
21 | }; |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
22 | |
21
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | // process.argv[0] seems to be the node executable itself |
30
3ac53da03439
Support loading TEMPLATE_PATH/katex_config.json.
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
24 | if(process.argv.length<=2 || process.argv.length>4){ |
3ac53da03439
Support loading TEMPLATE_PATH/katex_config.json.
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
25 | console.error(`Usage: ${process.argv[1]} input_file [katex_options.json]`) |
21
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | process.exit(1); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | } |
31
7b21ec8ec96a
fix weird nodejs length/index
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
28 | if(process.argv.length>3){ |
30
3ac53da03439
Support loading TEMPLATE_PATH/katex_config.json.
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
29 | katexOptions = JSON.parse(fs.readFileSync(process.argv[3])); |
3ac53da03439
Support loading TEMPLATE_PATH/katex_config.json.
Tuomo Valkonen <tuomov@iki.fi>
parents:
28
diff
changeset
|
30 | } |
21
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
31 | |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
32 | fs.readFile(process.argv[2], 'utf8', function (err, input) { |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
33 | var output, md; |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
34 | |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
35 | if(err){ |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
36 | console.error(err.stack || err.message || String(err)); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
37 | process.exit(1); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
38 | } |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
39 | |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
40 | try{ |
32
cef0a28afae0
Replace local links to .md by corresponding .html on build (markdown-it only).
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
41 | // Replace local links to .md by .html. |
33 | 42 | var re_nonlocal=/^[a-zA-Z0-9]*:\/\//; |
43 | var re_md_to_html=/^(.*)\.md$/; | |
21
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
44 | var md = require('markdown-it')({ |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
45 | html: true, |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | xhtmlOut: true, |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
47 | breaks: false, |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
48 | linkify: true, |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
49 | highlight: function (str, lang) { |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
50 | if (lang && hljs.getLanguage(lang)) { |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
51 | try{ |
40 | 52 | return hljs.highlight(str, {language: lang}).value; |
21
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
53 | } catch (__){} |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
54 | } |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
55 | return ''; |
32
cef0a28afae0
Replace local links to .md by corresponding .html on build (markdown-it only).
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
56 | }, |
cef0a28afae0
Replace local links to .md by corresponding .html on build (markdown-it only).
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
57 | replaceLink: function (link, env) { |
33 | 58 | if(link.match(re_nonlocal)){ |
59 | return link; | |
60 | }else{ | |
61 | return link.replace(re_md_to_html, "$1.html"); | |
62 | } | |
21
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
63 | } |
28
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
64 | }) |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
65 | var mk = require('@neilsustc/markdown-it-katex'); |
9b75d0f5e042
Use @neilsustc/markdown-it-katex instead of markdown-it-katex.
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
66 | var mm = require('markdown-it-mark'); |
32
cef0a28afae0
Replace local links to .md by corresponding .html on build (markdown-it only).
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
67 | var mr = require('markdown-it-replace-link'); |
36 | 68 | var mc = require('markdown-it-checkbox'); |
43 | 69 | var mcc = require('markdown-it-container'); |
70 | md.use(mk, katexOptions) | |
71 | .use(mm) | |
72 | .use(mr) | |
73 | .use(mc) | |
74 | .use(mcc, 'NOTE') | |
75 | .use(mcc, 'IMPORTANT'); | |
21
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
76 | output = md.render(input); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
77 | } catch(e) { |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
78 | console.error(e.stack || e.message || String(e)); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
79 | process.exit(1); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
80 | } |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
81 | |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
82 | process.stdout.write(output); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
83 | }); |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
84 | |
3c71c525cec2
Use markdown-it as default markdown parser.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
85 |