diff --git a/lib/handle/blockquote.js b/lib/handle/blockquote.js index fc8e556..100d40a 100644 --- a/lib/handle/blockquote.js +++ b/lib/handle/blockquote.js @@ -1,6 +1,11 @@ /** * @typedef {import('mdast').Blockquote} Blockquote - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions * @typedef {import('../util/indent-lines.js').Map} Map */ @@ -9,8 +14,11 @@ import {indentLines} from '../util/indent-lines.js' import {track} from '../util/track.js' /** - * @type {Handle} * @param {Blockquote} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function blockquote(node, _, context, safeOptions) { const exit = context.enter('blockquote') diff --git a/lib/handle/break.js b/lib/handle/break.js index 5f7a3bf..eb25406 100644 --- a/lib/handle/break.js +++ b/lib/handle/break.js @@ -1,15 +1,23 @@ /** - * @typedef {import('../types.js').Handle} Handle * @typedef {import('mdast').Break} Break + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {patternInScope} from '../util/pattern-in-scope.js' /** - * @type {Handle} * @param {Break} _ + * @param {Parent|undefined} _1 + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ -export function hardBreak(_, _1, context, safe) { +export function hardBreak(_, _1, context, safeOptions) { let index = -1 while (++index < context.unsafe.length) { @@ -19,7 +27,7 @@ export function hardBreak(_, _1, context, safe) { context.unsafe[index].character === '\n' && patternInScope(context.stack, context.unsafe[index]) ) { - return /[ \t]/.test(safe.before) ? '' : ' ' + return /[ \t]/.test(safeOptions.before) ? '' : ' ' } } diff --git a/lib/handle/code.js b/lib/handle/code.js index 5bfd398..1c8f8c3 100644 --- a/lib/handle/code.js +++ b/lib/handle/code.js @@ -1,7 +1,11 @@ /** * @typedef {import('mdast').Code} Code - * @typedef {import('../types.js').Handle} Handle - * @typedef {import('../types.js').Exit} Exit + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions * @typedef {import('../util/indent-lines.js').Map} Map */ @@ -13,8 +17,11 @@ import {safe} from '../util/safe.js' import {track} from '../util/track.js' /** - * @type {Handle} * @param {Code} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function code(node, _, context, safeOptions) { const marker = checkFence(context) diff --git a/lib/handle/definition.js b/lib/handle/definition.js index 65b21e0..74b3bd5 100644 --- a/lib/handle/definition.js +++ b/lib/handle/definition.js @@ -1,6 +1,11 @@ /** * @typedef {import('mdast').Definition} Definition - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {association} from '../util/association.js' @@ -9,8 +14,11 @@ import {safe} from '../util/safe.js' import {track} from '../util/track.js' /** - * @type {Handle} * @param {Definition} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function definition(node, _, context, safeOptions) { const quote = checkQuote(context) diff --git a/lib/handle/emphasis.js b/lib/handle/emphasis.js index 0cd613f..0c4e690 100644 --- a/lib/handle/emphasis.js +++ b/lib/handle/emphasis.js @@ -1,6 +1,11 @@ /** * @typedef {import('mdast').Emphasis} Emphasis - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {checkEmphasis} from '../util/check-emphasis.js' @@ -14,8 +19,11 @@ emphasis.peek = emphasisPeek // There’s no way around that though, except for injecting zero-width stuff. // Do we need to safeguard against that? /** - * @type {Handle} * @param {Emphasis} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function emphasis(node, _, context, safeOptions) { const marker = checkEmphasis(context) @@ -35,8 +43,10 @@ export function emphasis(node, _, context, safeOptions) { } /** - * @type {Handle} * @param {Emphasis} _ + * @param {Parent|undefined} _1 + * @param {Context} context + * @returns {string} */ function emphasisPeek(_, _1, context) { return context.options.emphasis || '*' diff --git a/lib/handle/heading.js b/lib/handle/heading.js index 0119868..57bdc7a 100644 --- a/lib/handle/heading.js +++ b/lib/handle/heading.js @@ -1,7 +1,11 @@ /** * @typedef {import('mdast').Heading} Heading - * @typedef {import('../types.js').Handle} Handle - * @typedef {import('../types.js').Exit} Exit + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {formatHeadingAsSetext} from '../util/format-heading-as-setext.js' @@ -9,8 +13,11 @@ import {containerPhrasing} from '../util/container-phrasing.js' import {track} from '../util/track.js' /** - * @type {Handle} * @param {Heading} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function heading(node, _, context, safeOptions) { const rank = Math.max(Math.min(6, node.depth || 1), 1) diff --git a/lib/handle/html.js b/lib/handle/html.js index b4f25f9..f82e1cc 100644 --- a/lib/handle/html.js +++ b/lib/handle/html.js @@ -1,20 +1,22 @@ /** * @typedef {import('mdast').HTML} HTML - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node */ html.peek = htmlPeek /** - * @type {Handle} * @param {HTML} node + * @returns {string} */ export function html(node) { return node.value || '' } /** - * @type {Handle} + * @returns {string} */ function htmlPeek() { return '<' diff --git a/lib/handle/image-reference.js b/lib/handle/image-reference.js index 5803f52..c880055 100644 --- a/lib/handle/image-reference.js +++ b/lib/handle/image-reference.js @@ -1,6 +1,11 @@ /** * @typedef {import('mdast').ImageReference} ImageReference - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {association} from '../util/association.js' @@ -10,8 +15,11 @@ import {track} from '../util/track.js' imageReference.peek = imageReferencePeek /** - * @type {Handle} * @param {ImageReference} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function imageReference(node, _, context, safeOptions) { const type = node.referenceType @@ -57,7 +65,7 @@ export function imageReference(node, _, context, safeOptions) { } /** - * @type {Handle} + * @returns {string} */ function imageReferencePeek() { return '!' diff --git a/lib/handle/image.js b/lib/handle/image.js index 1834e1b..7eae86d 100644 --- a/lib/handle/image.js +++ b/lib/handle/image.js @@ -1,6 +1,11 @@ /** * @typedef {import('mdast').Image} Image - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {checkQuote} from '../util/check-quote.js' @@ -10,8 +15,11 @@ import {track} from '../util/track.js' image.peek = imagePeek /** - * @type {Handle} * @param {Image} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function image(node, _, context, safeOptions) { const quote = checkQuote(context) @@ -74,7 +82,7 @@ export function image(node, _, context, safeOptions) { } /** - * @type {Handle} + * @returns {string} */ function imagePeek() { return '!' diff --git a/lib/handle/inline-code.js b/lib/handle/inline-code.js index e91631e..86568ca 100644 --- a/lib/handle/inline-code.js +++ b/lib/handle/inline-code.js @@ -1,6 +1,10 @@ /** * @typedef {import('mdast').InlineCode} InlineCode - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context */ import {patternCompile} from '../util/pattern-compile.js' @@ -8,8 +12,10 @@ import {patternCompile} from '../util/pattern-compile.js' inlineCode.peek = inlineCodePeek /** - * @type {Handle} * @param {InlineCode} node + * @param {Parent|undefined} _ + * @param {Context} context + * @returns {string} */ export function inlineCode(node, _, context) { let value = node.value || '' @@ -69,7 +75,7 @@ export function inlineCode(node, _, context) { } /** - * @type {Handle} + * @returns {string} */ function inlineCodePeek() { return '`' diff --git a/lib/handle/link-reference.js b/lib/handle/link-reference.js index e20b96b..3269b09 100644 --- a/lib/handle/link-reference.js +++ b/lib/handle/link-reference.js @@ -1,6 +1,11 @@ /** * @typedef {import('mdast').LinkReference} LinkReference - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {association} from '../util/association.js' @@ -11,8 +16,11 @@ import {track} from '../util/track.js' linkReference.peek = linkReferencePeek /** - * @type {Handle} * @param {LinkReference} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function linkReference(node, _, context, safeOptions) { const type = node.referenceType @@ -58,7 +66,7 @@ export function linkReference(node, _, context, safeOptions) { } /** - * @type {Handle} + * @returns {string} */ function linkReferencePeek() { return '[' diff --git a/lib/handle/link.js b/lib/handle/link.js index 69fce7f..4beb396 100644 --- a/lib/handle/link.js +++ b/lib/handle/link.js @@ -1,6 +1,11 @@ /** * @typedef {import('mdast').Link} Link - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions * @typedef {import('../types.js').Exit} Exit */ @@ -13,8 +18,11 @@ import {track} from '../util/track.js' link.peek = linkPeek /** - * @type {Handle} * @param {Link} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function link(node, _, context, safeOptions) { const quote = checkQuote(context) @@ -104,8 +112,10 @@ export function link(node, _, context, safeOptions) { } /** - * @type {Handle} * @param {Link} node + * @param {Parent|undefined} _ + * @param {Context} context + * @returns {string} */ function linkPeek(node, _, context) { return formatLinkAsAutolink(node, context) ? '<' : '[' diff --git a/lib/handle/list-item.js b/lib/handle/list-item.js index 2a61b3e..658b0b3 100644 --- a/lib/handle/list-item.js +++ b/lib/handle/list-item.js @@ -1,9 +1,12 @@ /** * @typedef {import('mdast').ListItem} ListItem - * @typedef {import('mdast').List} List * @typedef {import('../util/indent-lines.js').Map} Map - * @typedef {import('../types.js').Options} Options - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {checkBullet} from '../util/check-bullet.js' @@ -13,8 +16,11 @@ import {indentLines} from '../util/indent-lines.js' import {track} from '../util/track.js' /** - * @type {Handle} * @param {ListItem} node + * @param {Parent|undefined} parent + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function listItem(node, parent, context, safeOptions) { const listItemIndent = checkListItemIndent(context) diff --git a/lib/handle/list.js b/lib/handle/list.js index ca5c91b..2415f0e 100644 --- a/lib/handle/list.js +++ b/lib/handle/list.js @@ -1,6 +1,11 @@ /** * @typedef {import('mdast').List} List - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {containerFlow} from '../util/container-flow.js' @@ -11,8 +16,11 @@ import {checkBulletOrderedOther} from '../util/check-bullet-ordered-other.js' import {checkRule} from '../util/check-rule.js' /** - * @type {Handle} * @param {List} node + * @param {Parent|undefined} parent + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function list(node, parent, context, safeOptions) { const exit = context.enter('list') diff --git a/lib/handle/paragraph.js b/lib/handle/paragraph.js index 82b376b..be219ba 100644 --- a/lib/handle/paragraph.js +++ b/lib/handle/paragraph.js @@ -1,13 +1,21 @@ /** * @typedef {import('mdast').Paragraph} Paragraph - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {containerPhrasing} from '../util/container-phrasing.js' /** - * @type {Handle} * @param {Paragraph} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function paragraph(node, _, context, safeOptions) { const exit = context.enter('paragraph') diff --git a/lib/handle/root.js b/lib/handle/root.js index f1fcd1c..8653eec 100644 --- a/lib/handle/root.js +++ b/lib/handle/root.js @@ -1,13 +1,20 @@ /** * @typedef {import('mdast').Root} Root - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {containerFlow} from '../util/container-flow.js' /** - * @type {Handle} * @param {Root} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function root(node, _, context, safeOptions) { return containerFlow(node, context, safeOptions) diff --git a/lib/handle/strong.js b/lib/handle/strong.js index ee226b0..6edcd83 100644 --- a/lib/handle/strong.js +++ b/lib/handle/strong.js @@ -1,6 +1,11 @@ /** * @typedef {import('mdast').Strong} Strong - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {checkStrong} from '../util/check-strong.js' @@ -14,8 +19,11 @@ strong.peek = strongPeek // There’s no way around that though, except for injecting zero-width stuff. // Do we need to safeguard against that? /** - * @type {Handle} * @param {Strong} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function strong(node, _, context, safeOptions) { const marker = checkStrong(context) @@ -35,8 +43,10 @@ export function strong(node, _, context, safeOptions) { } /** - * @type {Handle} * @param {Strong} _ + * @param {Parent|undefined} _1 + * @param {Context} context + * @returns {string} */ function strongPeek(_, _1, context) { return context.options.strong || '*' diff --git a/lib/handle/text.js b/lib/handle/text.js index 83ac11c..f0823ac 100644 --- a/lib/handle/text.js +++ b/lib/handle/text.js @@ -1,13 +1,21 @@ /** * @typedef {import('mdast').Text} Text - * @typedef {import('../types.js').Handle} Handle + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {safe} from '../util/safe.js' /** - * @type {Handle} * @param {Text} node + * @param {Parent|undefined} _ + * @param {Context} context + * @param {SafeOptions} safeOptions + * @returns {string} */ export function text(node, _, context, safeOptions) { return safe(context, node.value, safeOptions) diff --git a/lib/handle/thematic-break.js b/lib/handle/thematic-break.js index 85ec3ff..5e6797c 100644 --- a/lib/handle/thematic-break.js +++ b/lib/handle/thematic-break.js @@ -1,14 +1,21 @@ /** - * @typedef {import('../types.js').Handle} Handle * @typedef {import('mdast').ThematicBreak} ThematicBreak + * @typedef {import('mdast').Root} Root + * @typedef {import('mdast').Content} Content + * @typedef {Root|Content} Node + * @typedef {Extract} Parent + * @typedef {import('../types.js').Context} Context + * @typedef {import('../types.js').SafeOptions} SafeOptions */ import {checkRuleRepetition} from '../util/check-rule-repetition.js' import {checkRule} from '../util/check-rule.js' /** - * @type {Handle} * @param {ThematicBreak} _ + * @param {Parent|undefined} _1 + * @param {Context} context + * @returns {string} */ export function thematicBreak(_, _1, context) { const value = ( diff --git a/lib/index.js b/lib/index.js index bdfcb8a..28fab1e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,9 +2,7 @@ * @typedef {import('./types.js').Node} Node * @typedef {import('./types.js').Options} Options * @typedef {import('./types.js').Context} Context - * @typedef {import('./types.js').Handle} Handle * @typedef {import('./types.js').Join} Join - * @typedef {import('./types.js').Unsafe} Unsafe */ import {zwitch} from 'zwitch' @@ -38,14 +36,13 @@ export function toMarkdown(tree, options = {}) { configure(context, {join: [joinDefinition]}) } - /** @type {Handle} */ context.handle = zwitch('type', { invalid, unknown, handlers: context.handlers }) - let result = context.handle(tree, null, context, { + let result = context.handle(tree, undefined, context, { before: '\n', after: '\n', now: {line: 1, column: 1}, @@ -74,7 +71,6 @@ export function toMarkdown(tree, options = {}) { } /** - * @type {Handle} * @param {unknown} value * @returns {never} */ @@ -83,7 +79,6 @@ function invalid(value) { } /** - * @type {Handle} * @param {unknown} node * @returns {never} */ diff --git a/lib/types.js b/lib/types.js index cd85c7f..41e1ffa 100644 --- a/lib/types.js +++ b/lib/types.js @@ -55,7 +55,7 @@ /** * @callback Handle * @param {any} node - * @param {Parent|null|undefined} parent + * @param {Parent|undefined} parent * @param {Context} context * @param {SafeOptions} safeOptions * @returns {string}