Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 24, 2023
1 parent 5d6077b commit 62ddace
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 150 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
*.d.ts
*.log
yarn.lock
!/index.d.ts
22 changes: 22 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {Root} from 'mdast'
import type {Plugin} from 'unified'
import type {Options} from './lib/index.js'

export type {Options} from './lib/index.js'

/**
* Add support for serializing to HTML.
*
* @this
* Unified processor.
* @param
* Configuration (optional).
* @returns
* Nothing.
*/
declare const remarkHtml: Plugin<
[(Readonly<Options> | null | undefined)?],
Root,
string
>
export default remarkHtml
5 changes: 1 addition & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
/**
* @typedef {import('./lib/index.js').Options} Options
*/

// Note: types exposed from `index.d.ts`.
export {default} from './lib/index.js'
86 changes: 46 additions & 40 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('hast-util-sanitize').Schema} Schema
*
* @typedef {import('hast-util-to-html').Options} ToHtmlOptions
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast-util-to-hast').Handlers} Handlers
* @typedef {import('unified').Compiler<Root, string>} Compiler
* @typedef {import('unified').Processor<undefined, undefined, undefined, Root, string>} Processor
*/

/**
* @typedef ExtraOptionsFields
* Configuration (optional).
* @property {boolean|Schema|null} [sanitize]
* How to sanitize the output.
* @property {import('mdast-util-to-hast').Handlers} [handlers={}]
* Object mapping mdast nodes to functions handling them.
* Extra fields.
* @property {Readonly<Handlers> | null | undefined} [handlers]
* How to turn mdast nodes into hast nodes (optional);
* passed to `mdast-util-to-hast`.
* @property {Readonly<Schema> | boolean | null | undefined} [sanitize]
* Sanitize the output, and how (default: `true`).
*
* @typedef {import('hast-util-to-html').Options & ExtraOptionsFields} Options
* @typedef {ToHtmlOptions & ExtraOptionsFields} Options
* Configuration.
*/

import {toHtml} from 'hast-util-to-html'
import {sanitize} from 'hast-util-sanitize'
import {toHast} from 'mdast-util-to-hast'
import {toHtml} from 'hast-util-to-html'

/** @type {Readonly<Options>} */
const emptyOptions = {}

/**
* Plugin to serialize markdown as HTML.
* Serialize markdown as HTML.
*
* @this {import('unified').Processor}
* @type {import('unified').Plugin<[Options?] | [], Root, string>}
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
export default function remarkHtml(settings = {}) {
const options = {...settings}
/** @type {boolean|undefined} */
let clean

if (typeof options.sanitize === 'boolean') {
clean = options.sanitize
// @ts-expect-error: to do: fix.
options.sanitize = undefined
}
export default function remarkHtml(options) {
/** @type {Processor} */
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
// eslint-disable-next-line unicorn/no-this-assignment
const self = this
const {handlers, sanitize: clean, ...toHtmlOptions} = options || emptyOptions
let allowDangerousHtml = false
/** @type {Readonly<Schema> | undefined} */
let schema

if (typeof clean !== 'boolean') {
clean = true
if (typeof clean === 'boolean') {
allowDangerousHtml = !clean
} else if (clean) {
schema = clean
}

Object.assign(this, {compiler})
self.compiler = compiler

/**
* @type {import('unified').Compiler<Root, string>}
* @type {Compiler}
*/
function compiler(node, file) {
const hast = toHast(node, {
allowDangerousHtml: !clean,
handlers: options.handlers
})
// @ts-expect-error: to do: no longer boolean.
const cleanHast = clean ? sanitize(hast, options.sanitize) : hast
const result = toHtml(
cleanHast,
Object.assign({}, options, {allowDangerousHtml: !clean})
)
function compiler(tree, file) {
const hast = toHast(tree, {handlers, allowDangerousHtml})
const safeHast = allowDangerousHtml ? hast : sanitize(hast, schema)
const result = toHtml(safeHast, {...toHtmlOptions, allowDangerousHtml})

if (file.extname) {
file.extname = '.html'
}

// Add an eof eol.
return node &&
node.type &&
node.type === 'root' &&
return tree &&
tree.type === 'root' &&
result &&
/[^\r\n]/.test(result.charAt(result.length - 1))
? result + '\n'
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@
},
"xo": {
"overrides": [
{
"files": [
"**/*.ts"
],
"rules": {
"@typescript-eslint/ban-types": "off"
}
},
{
"files": [
"test/**/*.js"
Expand Down
Loading

0 comments on commit 62ddace

Please sign in to comment.