Skip to content

Commit 62ddace

Browse files
committed
Refactor code-style
1 parent 5d6077b commit 62ddace

File tree

7 files changed

+216
-150
lines changed

7 files changed

+216
-150
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules/
44
*.d.ts
55
*.log
66
yarn.lock
7+
!/index.d.ts

index.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type {Root} from 'mdast'
2+
import type {Plugin} from 'unified'
3+
import type {Options} from './lib/index.js'
4+
5+
export type {Options} from './lib/index.js'
6+
7+
/**
8+
* Add support for serializing to HTML.
9+
*
10+
* @this
11+
* Unified processor.
12+
* @param
13+
* Configuration (optional).
14+
* @returns
15+
* Nothing.
16+
*/
17+
declare const remarkHtml: Plugin<
18+
[(Readonly<Options> | null | undefined)?],
19+
Root,
20+
string
21+
>
22+
export default remarkHtml

index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
/**
2-
* @typedef {import('./lib/index.js').Options} Options
3-
*/
4-
1+
// Note: types exposed from `index.d.ts`.
52
export {default} from './lib/index.js'

lib/index.js

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,73 @@
11
/**
2-
* @typedef {import('mdast').Root} Root
32
* @typedef {import('hast-util-sanitize').Schema} Schema
4-
*
3+
* @typedef {import('hast-util-to-html').Options} ToHtmlOptions
4+
* @typedef {import('mdast').Root} Root
5+
* @typedef {import('mdast-util-to-hast').Handlers} Handlers
6+
* @typedef {import('unified').Compiler<Root, string>} Compiler
7+
* @typedef {import('unified').Processor<undefined, undefined, undefined, Root, string>} Processor
8+
*/
9+
10+
/**
511
* @typedef ExtraOptionsFields
6-
* Configuration (optional).
7-
* @property {boolean|Schema|null} [sanitize]
8-
* How to sanitize the output.
9-
* @property {import('mdast-util-to-hast').Handlers} [handlers={}]
10-
* Object mapping mdast nodes to functions handling them.
12+
* Extra fields.
13+
* @property {Readonly<Handlers> | null | undefined} [handlers]
14+
* How to turn mdast nodes into hast nodes (optional);
15+
* passed to `mdast-util-to-hast`.
16+
* @property {Readonly<Schema> | boolean | null | undefined} [sanitize]
17+
* Sanitize the output, and how (default: `true`).
1118
*
12-
* @typedef {import('hast-util-to-html').Options & ExtraOptionsFields} Options
19+
* @typedef {ToHtmlOptions & ExtraOptionsFields} Options
20+
* Configuration.
1321
*/
1422

15-
import {toHtml} from 'hast-util-to-html'
1623
import {sanitize} from 'hast-util-sanitize'
1724
import {toHast} from 'mdast-util-to-hast'
25+
import {toHtml} from 'hast-util-to-html'
26+
27+
/** @type {Readonly<Options>} */
28+
const emptyOptions = {}
1829

1930
/**
20-
* Plugin to serialize markdown as HTML.
31+
* Serialize markdown as HTML.
2132
*
22-
* @this {import('unified').Processor}
23-
* @type {import('unified').Plugin<[Options?] | [], Root, string>}
33+
* @param {Readonly<Options> | null | undefined} [options]
34+
* Configuration (optional).
35+
* @returns {undefined}
36+
* Nothing.
2437
*/
25-
export default function remarkHtml(settings = {}) {
26-
const options = {...settings}
27-
/** @type {boolean|undefined} */
28-
let clean
29-
30-
if (typeof options.sanitize === 'boolean') {
31-
clean = options.sanitize
32-
// @ts-expect-error: to do: fix.
33-
options.sanitize = undefined
34-
}
38+
export default function remarkHtml(options) {
39+
/** @type {Processor} */
40+
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
41+
// eslint-disable-next-line unicorn/no-this-assignment
42+
const self = this
43+
const {handlers, sanitize: clean, ...toHtmlOptions} = options || emptyOptions
44+
let allowDangerousHtml = false
45+
/** @type {Readonly<Schema> | undefined} */
46+
let schema
3547

36-
if (typeof clean !== 'boolean') {
37-
clean = true
48+
if (typeof clean === 'boolean') {
49+
allowDangerousHtml = !clean
50+
} else if (clean) {
51+
schema = clean
3852
}
3953

40-
Object.assign(this, {compiler})
54+
self.compiler = compiler
4155

4256
/**
43-
* @type {import('unified').Compiler<Root, string>}
57+
* @type {Compiler}
4458
*/
45-
function compiler(node, file) {
46-
const hast = toHast(node, {
47-
allowDangerousHtml: !clean,
48-
handlers: options.handlers
49-
})
50-
// @ts-expect-error: to do: no longer boolean.
51-
const cleanHast = clean ? sanitize(hast, options.sanitize) : hast
52-
const result = toHtml(
53-
cleanHast,
54-
Object.assign({}, options, {allowDangerousHtml: !clean})
55-
)
59+
function compiler(tree, file) {
60+
const hast = toHast(tree, {handlers, allowDangerousHtml})
61+
const safeHast = allowDangerousHtml ? hast : sanitize(hast, schema)
62+
const result = toHtml(safeHast, {...toHtmlOptions, allowDangerousHtml})
5663

5764
if (file.extname) {
5865
file.extname = '.html'
5966
}
6067

6168
// Add an eof eol.
62-
return node &&
63-
node.type &&
64-
node.type === 'root' &&
69+
return tree &&
70+
tree.type === 'root' &&
6571
result &&
6672
/[^\r\n]/.test(result.charAt(result.length - 1))
6773
? result + '\n'

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@
9393
},
9494
"xo": {
9595
"overrides": [
96+
{
97+
"files": [
98+
"**/*.ts"
99+
],
100+
"rules": {
101+
"@typescript-eslint/ban-types": "off"
102+
}
103+
},
96104
{
97105
"files": [
98106
"test/**/*.js"

0 commit comments

Comments
 (0)