-
-
Notifications
You must be signed in to change notification settings - Fork 363
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
14 changed files
with
412 additions
and
248 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,12 +1,8 @@ | ||
coverage/ | ||
node_modules/ | ||
packages/remark/*.d.ts | ||
packages/remark-cli/*.d.ts | ||
packages/remark-parse/lib/*.d.ts | ||
packages/remark-parse/test.d.ts | ||
packages/remark-stringify/lib/*.d.ts | ||
packages/remark-stringify/test.d.ts | ||
test.d.ts | ||
.DS_Store | ||
*.d.ts | ||
*.log | ||
yarn.lock | ||
!/packages/remark-parse/index.d.ts | ||
!/packages/remark-stringify/index.d.ts |
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 |
---|---|---|
@@ -1,9 +1,28 @@ | ||
// This wrapper exists because JS in TS can’t export a `@type` of a function. | ||
import type {Root} from 'mdast' | ||
import type {Plugin} from 'unified' | ||
import type {Options} from './lib/index.js' | ||
|
||
declare const remarkParse: Plugin<[(Options | undefined)?], string, Root> | ||
export type {Options} from './lib/index.js' | ||
|
||
/** | ||
* Add support for parsing from markdown. | ||
* | ||
* @this | ||
* Unified processor. | ||
* @param | ||
* Configuration (optional). | ||
* @returns | ||
* Nothing. | ||
*/ | ||
declare const remarkParse: Plugin< | ||
[(Readonly<Options> | null | undefined)?], | ||
string, | ||
Root | ||
> | ||
export default remarkParse | ||
|
||
export type {Options} from './lib/index.js' | ||
// To do: register types. | ||
// // Add custom settings supported when `remark-parse` is added. | ||
// declare module 'unified' { | ||
// interface Settings extends 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
// Note: types exposed from `index.d.ts`. | ||
export {default} from './lib/index.js' |
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,33 +1,52 @@ | ||
/** | ||
* @typedef {import('mdast').Root} Root | ||
* @typedef {import('mdast-util-from-markdown').Options} Options | ||
* @typedef {import('mdast-util-from-markdown').Options} FromMarkdownOptions | ||
* @typedef {import('unified').Parser<Root>} Parser | ||
* @typedef {import('unified').Processor<Root>} Processor | ||
*/ | ||
|
||
/** | ||
* @typedef {Omit<FromMarkdownOptions, 'extensions' | 'fromMarkdownExtensions'>} Options | ||
*/ | ||
|
||
import {fromMarkdown} from 'mdast-util-from-markdown' | ||
|
||
/** | ||
* @this {import('unified').Processor} | ||
* @type {import('unified').Plugin<[Options?] | void[], string, Root>} | ||
* Aadd support for parsing from markdown. | ||
* | ||
* @param {Readonly<Options> | null | undefined} [options] | ||
* Configuration (optional). | ||
* @returns {undefined} | ||
* Nothing. | ||
*/ | ||
export default function remarkParse(options) { | ||
/** @type {import('unified').Parser<Root>} */ | ||
const parser = (doc) => { | ||
/** @type {Processor} */ | ||
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly. | ||
const self = this | ||
|
||
self.parser = parser | ||
|
||
/** | ||
* @type {Parser} | ||
*/ | ||
function parser(doc) { | ||
// To do: remove cast when typed. | ||
// Assume options. | ||
const settings = /** @type {Options} */ (this.data('settings')) | ||
const settings = /** @type {Options} */ (self.data('settings')) | ||
|
||
return fromMarkdown( | ||
doc, | ||
Object.assign({}, settings, options, { | ||
// Note: these options are not in the readme. | ||
// The goal is for them to be set by plugins on `data` instead of being | ||
// passed by users. | ||
// @ts-expect-error: to do: type. | ||
extensions: this.data('micromarkExtensions') || [], | ||
// @ts-expect-error: to do: type. | ||
mdastExtensions: this.data('fromMarkdownExtensions') || [] | ||
}) | ||
) | ||
} | ||
/** @type {FromMarkdownOptions} */ | ||
const resolvedOptions = { | ||
...settings, | ||
...options, | ||
// Note: these options are not in the readme. | ||
// The goal is for them to be set by plugins on `data` instead of being | ||
// passed by users. | ||
// @ts-expect-error: to do: type. | ||
extensions: self.data('micromarkExtensions') || [], | ||
// @ts-expect-error: to do: type. | ||
mdastExtensions: self.data('fromMarkdownExtensions') || [] | ||
} | ||
|
||
Object.assign(this, {Parser: parser}) | ||
return fromMarkdown(doc, resolvedOptions) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,28 @@ | ||
// This wrapper exists because JS in TS can’t export a `@type` of a function. | ||
import type {Root} from 'mdast' | ||
import type {Plugin} from 'unified' | ||
import type {Options} from './lib/index.js' | ||
|
||
declare const remarkStringify: Plugin<[(Options | undefined)?], Root, string> | ||
export type {Options} from './lib/index.js' | ||
|
||
/** | ||
* Add support for serializing as HTML. | ||
* | ||
* @this | ||
* Unified processor. | ||
* @param | ||
* Configuration (optional). | ||
* @returns | ||
* Nothing. | ||
*/ | ||
declare const remarkStringify: Plugin< | ||
[(Readonly<Options> | null | undefined)?], | ||
Root, | ||
string | ||
> | ||
export default remarkStringify | ||
|
||
export type {Options} from './lib/index.js' | ||
// To do: register types. | ||
// // Add custom settings supported when `remark-stringify` is added. | ||
// declare module 'unified' { | ||
// interface Settings extends 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
// Note: types exposed from `index.d.ts`. | ||
export {default} from './lib/index.js' |
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,36 +1,50 @@ | ||
/** | ||
* @typedef {import('mdast').Root|import('mdast').Content} Node | ||
* @typedef {import('mdast').Root} Root | ||
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownOptions | ||
* @typedef {import('unified').Compiler<Root, string>} Compiler | ||
* @typedef {import('unified').Processor<undefined, undefined, undefined, Root, string>} Processor | ||
*/ | ||
|
||
/** | ||
* @typedef {Omit<ToMarkdownOptions, 'extensions'>} Options | ||
*/ | ||
|
||
import {toMarkdown} from 'mdast-util-to-markdown' | ||
|
||
/** | ||
* @this {import('unified').Processor} | ||
* @type {import('unified').Plugin<[Options?]|void[], Node, string>} | ||
* Add support for serializing as markdown. | ||
* | ||
* @param {Readonly<Options> | null | undefined} [options] | ||
* Configuration (optional). | ||
* @returns {undefined} | ||
* Nothing. | ||
*/ | ||
export default function remarkStringify(options) { | ||
/** @type {import('unified').Compiler<Node, string>} */ | ||
const compiler = (tree) => { | ||
/** @type {Processor} */ | ||
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly. | ||
const self = this | ||
|
||
self.compiler = compiler | ||
|
||
/** | ||
* @type {Compiler} | ||
*/ | ||
function compiler(tree) { | ||
// To do: remove cast when typed. | ||
// Assume options. | ||
const settings = /** @type {Options} */ (this.data('settings')) | ||
const settings = /** @type {Options} */ (self.data('settings')) | ||
|
||
return toMarkdown( | ||
tree, | ||
Object.assign({}, settings, options, { | ||
// Note: this option is not in the readme. | ||
// The goal is for it to be set by plugins on `data` instead of being | ||
// passed by users. | ||
extensions: | ||
// @ts-expect-error: to do: type. | ||
/** @type {ToMarkdownOptions['extensions']} */ ( | ||
// @ts-expect-error: to do: type. | ||
this.data('toMarkdownExtensions') | ||
) || [] | ||
}) | ||
) | ||
} | ||
/** @type {ToMarkdownOptions} */ | ||
const resolvedOptions = { | ||
...settings, | ||
...options, | ||
// Note: this option is not in the readme. | ||
// The goal is for it to be set by plugins on `data` instead of being | ||
// passed by users. | ||
// @ts-expect-error: to do: type. | ||
extensions: self.data('toMarkdownExtensions') || [] | ||
} | ||
|
||
Object.assign(this, {Compiler: compiler}) | ||
return toMarkdown(tree, resolvedOptions) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import {unified} from 'unified' | ||
import remarkParse from 'remark-parse' | ||
import remarkStringify from 'remark-stringify' | ||
import {unified} from 'unified' | ||
|
||
/** | ||
* Create a new unified processor that already uses `remark-parse` and | ||
* `remark-stringify`. | ||
*/ | ||
export const remark = unified().use(remarkParse).use(remarkStringify).freeze() |
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
Oops, something went wrong.