|
1 | | -import fs from 'fs'; |
2 | | -import path from 'path'; |
3 | | -const MarkdownIt = require('markdown-it'); |
4 | | -const MarkdownItSub = require('markdown-it-sub'); |
5 | | -const MarkdownItSup = require('markdown-it-sup'); |
6 | | -const MarkdownItFootnote = require('markdown-it-footnote'); |
7 | | -const MarkdownItDeflist = require('markdown-it-deflist'); |
8 | | -const MarkdownItAbbr = require('markdown-it-abbr'); |
9 | | -const MarkdownItEmoji = require('markdown-it-emoji'); |
10 | | -const MarkdownItContainer = require('markdown-it-container'); |
11 | | -const MarkdownItIns = require('markdown-it-ins'); |
12 | | -const MarkdownItMark = require('markdown-it-mark'); |
13 | | -const MarkdownItImsize = require('markdown-it-imsize'); |
14 | | -const MarkdownItMathJax = require('markdown-it-mathjax'); |
15 | | -const MarkdownItTOC = require("markdown-it-table-of-contents"); |
16 | | -const MarkdownItAnchor = require('markdown-it-anchor'); |
17 | | -const MarkdownItRuby = require('markdown-it-ruby'); |
18 | | -const MarkdownItCheckbox = require('markdown-it-checkbox'); |
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +const MarkdownIt = require('markdown-it') |
| 4 | +const MarkdownItSub = require('markdown-it-sub') |
| 5 | +const MarkdownItSup = require('markdown-it-sup') |
| 6 | +const MarkdownItFootnote = require('markdown-it-footnote') |
| 7 | +const MarkdownItDeflist = require('markdown-it-deflist') |
| 8 | +const MarkdownItAbbr = require('markdown-it-abbr') |
| 9 | +const MarkdownItEmoji = require('markdown-it-emoji') |
| 10 | +const MarkdownItContainer = require('markdown-it-container') |
| 11 | +const MarkdownItIns = require('markdown-it-ins') |
| 12 | +const MarkdownItMark = require('markdown-it-mark') |
| 13 | +const MarkdownItImsize = require('markdown-it-imsize') |
| 14 | +const MarkdownItMathJax = require('markdown-it-mathjax') |
| 15 | +const MarkdownItTOC = require('markdown-it-table-of-contents') |
| 16 | +const MarkdownItAnchor = require('markdown-it-anchor') |
| 17 | +const MarkdownItRuby = require('markdown-it-ruby') |
| 18 | +const MarkdownItCheckbox = require('markdown-it-checkbox') |
19 | 19 |
|
20 | | -//https://hackmd.io/c/codimd-documentation/%2F%40codimd%2Fmarkdown-syntax |
| 20 | +// https://hackmd.io/c/codimd-documentation/%2F%40codimd%2Fmarkdown-syntax |
21 | 21 | const md = new MarkdownIt({ |
22 | | - html: true, |
23 | | - breaks: true, |
24 | | - linkify: true, |
25 | | - typographer: true |
| 22 | + html: true, |
| 23 | + breaks: true, |
| 24 | + linkify: true, |
| 25 | + typographer: true |
26 | 26 | }) |
27 | | - .use(MarkdownItMathJax()) |
28 | | - .use(MarkdownItSub) |
29 | | - .use(MarkdownItSup) |
30 | | - .use(MarkdownItFootnote) |
31 | | - .use(MarkdownItDeflist) |
32 | | - .use(MarkdownItAbbr) |
33 | | - .use(MarkdownItMark) |
34 | | - .use(MarkdownItEmoji) |
35 | | - .use(MarkdownItContainer, 'success') |
36 | | - .use(MarkdownItContainer, 'info') |
37 | | - .use(MarkdownItContainer, 'warning') |
38 | | - .use(MarkdownItContainer, 'danger') |
39 | | - .use(MarkdownItIns) |
40 | | - .use(MarkdownItImsize) |
41 | | - .use(MarkdownItTOC, { |
42 | | - markerPattern: /^\[toc\]/im, |
43 | | - includeLevel: [1, 2, 3, 4] |
44 | | - }) |
45 | | - .use(MarkdownItAnchor) |
46 | | - .use(MarkdownItRuby) |
47 | | - .use(MarkdownItCheckbox,{ |
48 | | - divWrap: true |
49 | | - }) |
50 | | - ; |
| 27 | + .use(MarkdownItMathJax()) |
| 28 | + .use(MarkdownItSub) |
| 29 | + .use(MarkdownItSup) |
| 30 | + .use(MarkdownItFootnote) |
| 31 | + .use(MarkdownItDeflist) |
| 32 | + .use(MarkdownItAbbr) |
| 33 | + .use(MarkdownItMark) |
| 34 | + .use(MarkdownItEmoji) |
| 35 | + .use(MarkdownItContainer, 'success') |
| 36 | + .use(MarkdownItContainer, 'info') |
| 37 | + .use(MarkdownItContainer, 'warning') |
| 38 | + .use(MarkdownItContainer, 'danger') |
| 39 | + .use(MarkdownItIns) |
| 40 | + .use(MarkdownItImsize) |
| 41 | + .use(MarkdownItTOC, { |
| 42 | + markerPattern: /^\[toc\]/im, |
| 43 | + includeLevel: [1, 2, 3, 4] |
| 44 | + }) |
| 45 | + .use(MarkdownItAnchor) |
| 46 | + .use(MarkdownItRuby) |
| 47 | + .use(MarkdownItCheckbox, { |
| 48 | + divWrap: true |
| 49 | + }) |
51 | 50 |
|
52 | 51 | export class Convert { |
53 | | - src: Array<string>; |
54 | | - dest: string; |
55 | | - layout: string; |
| 52 | + src: Array<string> |
| 53 | + dest: string |
| 54 | + layout: string |
| 55 | + |
| 56 | + constructor (src: Array<string>, dest: string, layout: string) { |
| 57 | + this.src = src |
| 58 | + this.dest = dest |
| 59 | + this.layout = layout |
| 60 | + } |
56 | 61 |
|
57 | | - constructor(src: Array<string>, dest: string, layout: string) { |
58 | | - this.src = src; |
59 | | - this.dest = dest; |
60 | | - this.layout = layout; |
| 62 | + // @param html: html string |
| 63 | + // @return: html string with layout |
| 64 | + private addLayout (html: string): string { |
| 65 | + if (fs.existsSync(this.layout)) { |
| 66 | + const layout = fs.readFileSync(this.layout, { encoding: 'utf-8' }) |
| 67 | + return layout.replace('{{main}}', html) |
61 | 68 | } |
62 | 69 |
|
63 | | - // @param html: html string |
64 | | - // @return: html string with layout |
65 | | - private addLayout(html: string): string { |
66 | | - if (fs.existsSync(this.layout)) { |
67 | | - let layout = fs.readFileSync(this.layout, { encoding: 'utf-8' }); |
68 | | - return layout.replace('{{main}}', html); |
69 | | - } |
| 70 | + console.error(`${this.layout} is not found`) |
| 71 | + return html |
| 72 | + } |
70 | 73 |
|
71 | | - console.error(`${this.layout} is not found`); |
72 | | - return html; |
73 | | - } |
| 74 | + // @param filepath: the path of the file should be converted |
| 75 | + // this function doesn't check the ext name of filepath |
| 76 | + public convertFile (filepath: string) { |
| 77 | + const markdown = fs.readFileSync(filepath, { encoding: 'utf-8' }) |
| 78 | + const html = md.render(markdown) |
| 79 | + const res = this.addLayout(html) |
| 80 | + const basename = path.basename(filepath) |
| 81 | + fs.writeFileSync(path.join(this.dest, basename.replace(/\.md$/, '.html')), res) |
| 82 | + } |
74 | 83 |
|
75 | | - // @param filepath: the path of the file should be converted |
76 | | - // this function doesn't check the ext name of filepath |
77 | | - public convertFile(filepath: string) { |
78 | | - let markdown = fs.readFileSync(filepath, { encoding: 'utf-8' }); |
79 | | - let html = md.render(markdown); |
80 | | - let res = this.addLayout(html); |
81 | | - let basename = path.basename(filepath); |
82 | | - fs.writeFileSync(path.join(this.dest, basename.replace(/\.md$/, '.html')), res); |
| 84 | + public convertBatch () { |
| 85 | + if (!fs.existsSync(this.dest)) { |
| 86 | + fs.mkdirSync(this.dest) |
83 | 87 | } |
| 88 | + this.src.forEach((fileOrDir: string) => { |
| 89 | + if (!fs.existsSync(fileOrDir)) { |
| 90 | + console.error(`${fileOrDir} is not found`) |
| 91 | + return |
| 92 | + } |
84 | 93 |
|
85 | | - public convertBatch() { |
86 | | - if (!fs.existsSync(this.dest)) { |
87 | | - fs.mkdirSync(this.dest); |
88 | | - } |
89 | | - this.src.forEach((fileOrDir: string) => { |
90 | | - if (!fs.existsSync(fileOrDir)) { |
91 | | - console.error(`${fileOrDir} is not found`); |
92 | | - return; |
93 | | - } |
94 | | - |
95 | | - let stats = fs.statSync(fileOrDir); |
| 94 | + const stats = fs.statSync(fileOrDir) |
96 | 95 |
|
97 | | - if (stats.isDirectory()) { |
98 | | - fs.readdir(fileOrDir, (err: any, files: Array<string>) => { |
99 | | - if (err) { |
100 | | - throw (err); |
101 | | - } |
102 | | - files?.forEach(fn => { |
103 | | - if (path.extname(fn) === '.md') { |
104 | | - this.convertFile(path.join(fileOrDir, fn)); |
105 | | - } |
106 | | - }); |
107 | | - }); |
108 | | - } else if (stats.isFile()) { |
109 | | - if (path.extname(fileOrDir) === '.md') { |
110 | | - this.convertFile(fileOrDir); |
111 | | - } |
| 96 | + if (stats.isDirectory()) { |
| 97 | + fs.readdir(fileOrDir, (err: any, files: Array<string>) => { |
| 98 | + if (err) { |
| 99 | + throw (err) |
| 100 | + } |
| 101 | + files?.forEach((fn) => { |
| 102 | + if (path.extname(fn) === '.md') { |
| 103 | + this.convertFile(path.join(fileOrDir, fn)) |
112 | 104 | } |
113 | | - }); |
114 | | - } |
| 105 | + }) |
| 106 | + }) |
| 107 | + } else if (stats.isFile()) { |
| 108 | + if (path.extname(fileOrDir) === '.md') { |
| 109 | + this.convertFile(fileOrDir) |
| 110 | + } |
| 111 | + } |
| 112 | + }) |
| 113 | + } |
115 | 114 | } |
0 commit comments