-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
64 lines (58 loc) · 1.99 KB
/
index.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import prettier from "prettier";
import remark from "remark";
import listItem from "./listItem";
import { relativeLinks } from "./relativeLinks";
/**
* 与えられたMarkdownテキストを整形して返す
* @param markdownText 整形前のMarkdownテキスト
* @param options オプション
* @return 整形済みのMarkdownテキスト
*/
function format(markdownText: string, options: FormatOptions): string {
const { team } = options;
markdownText = formatUsingPrettier(markdownText);
markdownText = formatUsingRemark(markdownText, { team });
markdownText = newlineCRLF(markdownText); // esaのMarkdownの改行コードはCRLFで、LFに直してもesa側でCRLFに直されるので、CRLFに統一する
return markdownText;
}
/**
* 整形オプション
*/
export type FormatOptions = {
/**
* esaチーム名
*
* esaチームのURL`https://***.esa.io`の`***`の部分。ここで指定したチーム内の絶対URLリンクを相対URLリンクに置き換えます。
*/
readonly team: string;
};
function formatUsingPrettier(markdownText: string): string {
return prettier.format(markdownText, {
parser: "markdown-nocjsp", // prettier本家のMarkdownパーサーは英単語の前後にスペースを入れる問題があるので、この問題を対策がされたパーサーを用いています。
plugins: [require("prettier-plugin-md-nocjsp")],
});
}
function formatUsingRemark(
markdownText: string,
{ team }: { readonly team: string }
): string {
const file = remark()
.use(require("remark-gfm"))
.use({
settings: {
fences: true,
bullet: "-",
listItemIndent: "one",
handlers: { listItem },
},
})
.use(relativeLinks, {
domainRegex: new RegExp(`http[s]*:\\/\\/${team}\\.esa\\.io[/]?`),
})
.processSync(markdownText);
return file.toString();
}
function newlineCRLF(markdownText: string): string {
return markdownText.replace(/\r\n|\r|\n/g, "\r\n");
}
export default format;