forked from get-alex/alex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (130 loc) · 3.26 KB
/
index.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @typedef {import('nlcst').Root} Root
* @typedef {import('./filter.js').Options} FilterOptions
*
* @typedef {boolean|undefined} NoBinaryOption
* @typedef {0|1|2|undefined} SurenessOption
*
* @typedef {{noBinary: NoBinaryOption, sureness: SurenessOption}} TextOptions
*
* @typedef {{noBinary?: NoBinaryOption, profanitySureness?: SurenessOption} & FilterOptions} OptionsObject
* @typedef {import('vfile').VFileCompatible} Input
* @typedef {OptionsObject|string[]|undefined} Options
*/
import {VFile} from 'vfile'
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkGfm from 'remark-gfm'
import remarkFrontmatter from 'remark-frontmatter'
import remarkMdx from 'remark-mdx'
import rehypeParse from 'rehype-parse'
import retextEnglish from 'retext-english'
import retextEquality from 'retext-equality'
import retextProfanities from 'retext-profanities'
import remarkRetext from 'remark-retext'
import rehypeRetext from 'rehype-retext'
import {sort} from 'vfile-sort'
import {filter} from './filter.js'
/** @param {TextOptions} options */
function makeText(options) {
return unified()
.use(retextEnglish)
.use(retextEquality, options)
.use(retextProfanities, options)
}
/**
* Alex’s core.
*
* @param {Input} value
* @param {FilterOptions} options
* @param {import('unified').Processor<void, Root>} processor
*/
function core(value, options, processor) {
const file = new VFile(value)
const tree = processor.use(filter, options).parse(file)
processor.runSync(tree, file)
sort(file)
return file
}
export default markdown
/**
* Alex (markdown).
*
* @param {Input} value
* @param {Options} [config]
*/
export function markdown(value, config) {
const options = splitOptions(config)
return core(
value,
options.filter,
unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkFrontmatter, ['yaml', 'toml'])
.use(remarkRetext, makeText(options.text))
)
}
/**
* Alex (MDX).
*
* @param {Input} value
* @param {Options} [config]
*/
export function mdx(value, config) {
const options = splitOptions(config)
return core(
value,
options.filter,
unified()
.use(remarkParse)
.use(remarkMdx)
.use(remarkRetext, makeText(options.text))
)
}
/**
* Alex (HTML).
*
* @param {Input} value
* @param {Options} [config]
*/
export function html(value, config) {
const options = splitOptions(config)
return core(
value,
options.filter,
unified().use(rehypeParse).use(rehypeRetext, makeText(options.text))
)
}
/**
* Alex (plain text).
*
* @param {Input} value
* @param {Options} [config]
*/
export function text(value, config) {
const options = splitOptions(config)
return core(value, options.filter, makeText(options.text))
}
/**
* @param {Options} options
*/
function splitOptions(options) {
/** @type {string[]|undefined} */
let allow
/** @type {string[]|undefined} */
let deny
/** @type {boolean|undefined} */
let noBinary
/** @type {SurenessOption} */
let sureness
if (Array.isArray(options)) {
allow = options
} else if (options) {
allow = options.allow
deny = options.deny
noBinary = options.noBinary
sureness = options.profanitySureness
}
return {filter: {allow, deny}, text: {noBinary, sureness}}
}