Skip to content

Commit 97417a4

Browse files
committed
Refactor code-style
* Add support for `null` in API input types * Only pass `undefined` around internally, not `null` * Add more docs to JSDoc * Refactor a *ton* of code * Rename `entities` option to `characterReferences` * Add exports of useful types (`CharacterReferences`, `Quote`, `Space`)
1 parent 37bd298 commit 97417a4

31 files changed

+1074
-661
lines changed

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/**
2+
* @typedef {import('./lib/types.js').CharacterReferences} CharacterReferences
23
* @typedef {import('./lib/types.js').Options} Options
4+
* @typedef {import('./lib/types.js').Quote} Quote
5+
* @typedef {import('./lib/types.js').Space} Space
36
*/
47

58
export {toHtml} from './lib/index.js'

lib/comment.js

-32
This file was deleted.

lib/constants.js

-26
This file was deleted.

lib/doctype.js

-15
This file was deleted.

lib/handle/comment.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @typedef {import('../types.js').Comment} Comment
3+
* @typedef {import('../types.js').Parent} Parent
4+
* @typedef {import('../types.js').State} State
5+
*/
6+
7+
import {stringifyEntities} from 'stringify-entities'
8+
9+
/**
10+
* Serialize a comment.
11+
*
12+
* @param {Comment} node
13+
* Node to handle.
14+
* @param {number | undefined} _1
15+
* Index of `node` in `parent.
16+
* @param {Parent | undefined} _2
17+
* Parent of `node`.
18+
* @param {State} state
19+
* Info passed around about the current state.
20+
* @returns {string}
21+
* Serialized node.
22+
*/
23+
export function comment(node, _1, _2, state) {
24+
// See: <https://html.spec.whatwg.org/multipage/syntax.html#comments>
25+
return state.settings.bogusComments
26+
? '<?' +
27+
stringifyEntities(
28+
node.value,
29+
Object.assign({}, state.settings.characterReferences, {subset: ['>']})
30+
) +
31+
'>'
32+
: '<!--' + node.value.replace(/^>|^->|<!--|-->|--!>|<!-$/g, encode) + '-->'
33+
34+
/**
35+
* @param {string} $0
36+
*/
37+
function encode($0) {
38+
return stringifyEntities(
39+
$0,
40+
Object.assign({}, state.settings.characterReferences, {
41+
subset: ['<', '>']
42+
})
43+
)
44+
}
45+
}

lib/handle/doctype.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @typedef {import('../types.js').DocType} DocType
3+
* @typedef {import('../types.js').Parent} Parent
4+
* @typedef {import('../types.js').State} State
5+
*/
6+
7+
/**
8+
* Serialize a doctype.
9+
*
10+
* @param {DocType} _1
11+
* Node to handle.
12+
* @param {number | undefined} _2
13+
* Index of `node` in `parent.
14+
* @param {Parent | undefined} _3
15+
* Parent of `node`.
16+
* @param {State} state
17+
* Info passed around about the current state.
18+
* @returns {string}
19+
* Serialized node.
20+
*/
21+
export function doctype(_1, _2, _3, state) {
22+
return (
23+
'<!' +
24+
(state.settings.upperDoctype ? 'DOCTYPE' : 'doctype') +
25+
(state.settings.tightDoctype ? '' : ' ') +
26+
'html>'
27+
)
28+
}

0 commit comments

Comments
 (0)