-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
91 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
coverage/ | ||
node_modules/ | ||
.DS_Store | ||
*.d.ts | ||
*.log | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,61 @@ | ||
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('hast').Root} HastRoot | ||
* @typedef {import('mdast').Root} MdastRoot | ||
* @typedef {import('mdast-util-to-hast').Options} Options | ||
* @typedef {import('unified').Processor<any, any, any, any>} Processor | ||
*/ | ||
|
||
import {toHast} from 'mdast-util-to-hast' | ||
|
||
// Attacher. | ||
// If a destination is given, runs the destination with the new hast tree | ||
// (bridge mode). | ||
// Without destination, returns the tree: further plugins run on that tree | ||
// (mutate mode). | ||
export default function remarkRehype(destination, options) { | ||
if (destination && !destination.process) { | ||
options = destination | ||
destination = null | ||
} | ||
// Note: the `<MdastRoot, HastRoot>` overload doesn’t seem to work :'( | ||
|
||
return destination ? bridge(destination, options) : mutate(options) | ||
} | ||
|
||
// Bridge mode. | ||
// Runs the destination with the new hast tree. | ||
function bridge(destination, options) { | ||
return transformer | ||
/** | ||
* Plugin to bridge or mutate to rehype. | ||
* | ||
* If a destination is given, runs the destination with the new hast tree | ||
* (bridge-mode). | ||
* Without destination, returns the hast tree: further plugins run on that tree | ||
* (mutate-mode). | ||
* | ||
* @param destination | ||
* Optional unified processor. | ||
* @param options | ||
* Options passed to `mdast-util-to-hast`. | ||
*/ | ||
const remarkRehype = | ||
/** @type {(import('unified').Plugin<[Processor, Options?]|[Options]|[], MdastRoot>)} */ | ||
( | ||
function (destination, options) { | ||
return destination && 'run' in destination | ||
? bridge(destination, options) | ||
: mutate(destination) | ||
} | ||
) | ||
|
||
function transformer(node, file, next) { | ||
destination.run(toHast(node, options), file, done) | ||
export default remarkRehype | ||
|
||
function done(error) { | ||
/** | ||
* Bridge-mode. | ||
* Runs the destination with the new hast tree. | ||
* | ||
* @type {import('unified').Plugin<[Processor, Options?], MdastRoot>} | ||
*/ | ||
function bridge(destination, options) { | ||
return (node, file, next) => { | ||
destination.run(toHast(node, options), file, (error) => { | ||
next(error) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Mutate-mode. | ||
// Further transformers run on the hast tree. | ||
/** | ||
* Mutate-mode. | ||
* Further transformers run on the nlcst tree. | ||
* | ||
* @type {import('unified').Plugin<[Options?]|void[], MdastRoot, HastRoot>} | ||
*/ | ||
function mutate(options) { | ||
return transformer | ||
|
||
function transformer(node) { | ||
return toHast(node, options) | ||
} | ||
// @ts-expect-error: assume a corresponding node is returned for `toHast`. | ||
return (node) => toHast(node, options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"include": ["*.js"], | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"lib": ["ES2020"], | ||
"module": "ES2020", | ||
"moduleResolution": "node", | ||
"allowJs": true, | ||
"checkJs": true, | ||
"declaration": true, | ||
"emitDeclarationOnly": true, | ||
"allowSyntheticDefaultImports": true, | ||
"skipLibCheck": true, | ||
"strict": true | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.