Skip to content

Commit 524d142

Browse files
committed
Use ESlint.
Now we can run `npm run lint` to format the source code.
1 parent 14d674a commit 524d142

File tree

6 files changed

+4362
-380
lines changed

6 files changed

+4362
-380
lines changed

.eslintrc.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
env:
2+
browser: true
3+
commonjs: true
4+
es2021: true
5+
extends:
6+
- standard
7+
parser: '@typescript-eslint/parser'
8+
parserOptions:
9+
ecmaVersion: latest
10+
plugins:
11+
- '@typescript-eslint'
12+
rules: {}

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ A simple Node.js wrapper for `markdown-it`. This tool helps to convert HackMD ma
1111
npm install -g hackmd-to-html-cli
1212
```
1313

14-
See demo: [https://liao2000.github.io/hackmd-to-html-cli/](https://liao2000.github.io/hackmd-to-html-cli/)
14+
Input: [./example/index.md](https://raw.githubusercontent.com/liao2000/hackmd-to-html-cli/main/example/index.md)
15+
16+
Output: [https://liao2000.github.io/hackmd-to-html-cli/](https://liao2000.github.io/hackmd-to-html-cli/)
1517

1618
## Usage
1719

@@ -84,9 +86,14 @@ $ hmd2html -s hello.md -l ./myLayout.html
8486
</html>
8587
```
8688

89+
## Develop
90+
91+
1. `npm run lint` to check the format of source code.
92+
2. `npm test` to test this package, which generates result from `./example` into `./output`.
93+
8794
## TODO
8895

89-
+ Provide more template & styles
96+
+ Provide more templates & styles
9097
+ Automatically generate HTML `<title>`
9198
+ Support more HackMD [syntax](https://hackmd.io/features-tw?both)
9299
+ Graphviz

lib/converter.ts

Lines changed: 99 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,114 @@
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')
1919

20-
//https://hackmd.io/c/codimd-documentation/%2F%40codimd%2Fmarkdown-syntax
20+
// https://hackmd.io/c/codimd-documentation/%2F%40codimd%2Fmarkdown-syntax
2121
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
2626
})
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+
})
5150

5251
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+
}
5661

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)
6168
}
6269

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+
}
7073

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+
}
7483

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)
8387
}
88+
this.src.forEach((fileOrDir: string) => {
89+
if (!fs.existsSync(fileOrDir)) {
90+
console.error(`${fileOrDir} is not found`)
91+
return
92+
}
8493

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)
9695

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))
112104
}
113-
});
114-
}
105+
})
106+
})
107+
} else if (stats.isFile()) {
108+
if (path.extname(fileOrDir) === '.md') {
109+
this.convertFile(fileOrDir)
110+
}
111+
}
112+
})
113+
}
115114
}

lib/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env node
2-
import commander from 'commander';
3-
import path from 'path';
4-
import { Convert } from './converter';
2+
import commander from 'commander'
3+
import path from 'path'
4+
import { Convert } from './converter'
55

6-
commander.program.version('0.0.4', '-v, --version', 'output the current version');
6+
commander.program.version('0.0.4', '-v, --version', 'output the current version')
77
commander.program
8-
.requiredOption('-s, --source <files_or_dirs...>', 'specify the input markdown files or directories')
9-
.addOption(new commander.Option('-d, --destination <path>', 'specify the output directory').default('./output', './output'))
10-
.addOption(new commander.Option('-l, --layout <html_file>', 'specify the layout file').default('', '""'))
11-
.parse(process.argv);
8+
.requiredOption('-s, --source <files_or_dirs...>', 'specify the input markdown files or directories')
9+
.addOption(new commander.Option('-d, --destination <path>', 'specify the output directory').default('./output', './output'))
10+
.addOption(new commander.Option('-l, --layout <html_file>', 'specify the layout file').default('', '""'))
11+
.parse(process.argv)
1212

13-
const options = commander.program.opts();
13+
const options = commander.program.opts()
1414

15-
const dest: string = options.destination == '' ? './output' : options.destination;
16-
const layout: string = options.layout == '' ? path.join(__dirname, '../layout.html') : options.layout;
17-
new Convert(options.source, dest, layout).convertBatch();
15+
const dest: string = options.destination === '' ? './output' : options.destination
16+
const layout: string = options.layout === '' ? path.join(__dirname, '../layout.html') : options.layout
17+
new Convert(options.source, dest, layout).convertBatch()

0 commit comments

Comments
 (0)