UNPKG

mdast-util-wikirefs

Version:

Converts a `micromark` token stream into an `mdast` syntax tree.

197 lines 6.78 kB
/// <reference types="unist" /> import { WikiRefsOptions, AttrData, WikiLinkData, WikiEmbedData } from "micromark-extension-wikirefs"; // from: https://github.com/syntax-tree/mdast-util-to-hast#types import * as Uni from "unist"; import { Extension as FromMarkdownExtension } from "mdast-util-from-markdown"; import { Options as ToMarkdownExtension } from "mdast-util-to-markdown"; // Add custom data tracked to turn markdown into a tree. declare module "mdast-util-from-markdown" { // eslint-disable-next-line @typescript-eslint/consistent-type-definitions interface CompileData { /** * track the current attribute key */ curKey?: string; } } // a note on duplicate node data: // // it might feel like some data properties commit data duplicatation // (because they do), but this is intentional. // // the 'item' data property provides the end-user with a more direct means of // inspecting the shape of the wikiref data, as opposed to sifting through // html properties (hName, hProperties, hChildren...) to figure it out. // attrs / wikiattr // note: this node simply holds data from markdown caml-attributes, // which is used to build the html attrbox interface AttrBoxDataNode extends Uni.Data { type: "attrbox-data"; data: { items: AttrData; }; } interface AttrBoxNode extends Uni.Parent { type: "attrbox"; children: (AttrBoxTitleNode | AttrBoxListNode)[]; data: { items: AttrData; // rehype properties hName: "aside"; hProperties: any; }; } interface AttrBoxTitleNode extends Uni.Parent { type: "attrbox-title"; children: any[]; // text node: https://github.com/syntax-tree/mdast#text data: { hName: "span"; hProperties: any | null; }; } interface AttrBoxListNode extends Uni.Parent { type: "attrbox-list"; children: (AttrKeyNode | AttrValNode)[]; data: { hName: "dl"; }; } interface AttrKeyNode extends Uni.Parent { type: "attr-key"; children: any[]; // text node: https://github.com/syntax-tree/mdast#text data: { hName: "dt"; }; } interface AttrValNode extends Uni.Parent { type: "attr-val"; children: (WikiAttrNode | any)[]; // ...or caml's 'AttrDataPrimitive' (using 'any' to avoid importing caml) data: { hName: "dd"; }; } // wikiref nodes interface WikiAttrNode extends Uni.Parent { type: "wikiattr"; children: any[]; // text node: https://github.com/syntax-tree/mdast#text data: { // 'item' data stored in top-level 'AttrBoxNode' hName: "a"; hProperties: any | null; }; } // wikilink interface WikiLinkNode extends Uni.Parent { type: "wikilink"; children: any[]; // text node: https://github.com/syntax-tree/mdast#text data: { item: WikiLinkData; hName: "a"; hProperties: any | null; }; } // wikiembed // markdown // abstract node interface WikiEmbedNode extends Uni.Parent { type: "wikiembed"; children: any[]; // children: (EmbedMkdnWrapperNode | // EmbedMediaSpanNode)[]; data: { item: WikiEmbedData; hName: "p"; }; } interface EmbedMkdnWrapperNode extends Uni.Parent { type: "embed-mkdn-wrapper"; children: (EmbedMkdnTitleNode | EmbedMkdnLinkNode | EmbedMkdnContentNode)[]; data: { // rehype properties hName: "div"; hProperties: any | null; }; } interface EmbedMkdnTitleNode extends Uni.Parent { type: "embed-mkdn-title"; children: any[]; // <a> data: { hName: "div"; hProperties: any | null; }; } interface EmbedMkdnLinkNode extends Uni.Parent { type: "embed-mkdn-link"; children: any[]; // <a> data: { hName: "div"; hProperties: any; // <a> }; } interface EmbedMkdnContentNode extends Uni.Parent { type: "embed-mkdn-content"; children: any[]; // embed content... data: { hName: "div"; hProperties: any | null; }; } // media interface EmbedMediaSpanNode extends Uni.Parent { type: "embed-media-span"; children: (EmbedMediaAudioNode | EmbedMediaImageNode | EmbedMediaVideoNode)[]; data: { // rehype properties hName: "span"; hProperties: any | null; }; } interface EmbedMediaAudioNode extends Uni.Parent { type: "embed-media-audio"; data: { hName: "audio"; hProperties: any | null; }; } interface EmbedMediaImageNode extends Uni.Parent { type: "embed-media-image"; data: { hName: "img"; hProperties: any | null; }; } interface EmbedMediaVideoNode extends Uni.Parent { type: "embed-media-video"; data: { hName: "video"; hProperties: any | null; }; } declare function initWikiAttrBox(tree: Uni.Node, opts?: Partial<WikiRefsOptions>): AttrBoxNode | undefined; declare function fromMarkdownWikiAttrs(opts?: Partial<WikiRefsOptions>): FromMarkdownExtension; declare function fromMarkdownWikiLinks(opts?: Partial<WikiRefsOptions>): FromMarkdownExtension; declare function fromMarkdownWikiEmbeds(opts?: Partial<WikiRefsOptions>): FromMarkdownExtension; declare function fromMarkdownWikiRefs(opts?: Partial<WikiRefsOptions>): FromMarkdownExtension[]; declare function toMarkdownWikiAttrs(opts?: Partial<WikiRefsOptions>): ToMarkdownExtension; // eslint-disable-next-line @typescript-eslint/no-unused-vars declare function toMarkdownWikiLinks(opts?: Partial<WikiRefsOptions>): ToMarkdownExtension; // eslint-disable-next-line @typescript-eslint/no-unused-vars declare function toMarkdownWikiEmbeds(opts?: Partial<WikiRefsOptions>): ToMarkdownExtension; declare function toMarkdownWikiRefs(opts?: Partial<WikiRefsOptions>): ToMarkdownExtension; type Visitor<V extends Uni.Node> = (node: V) => VisitorAction | void; declare enum VisitorAction { /** Continue traversal as normal. */ CONTINUE = 1, /** Do not traverse this node's children. */ SKIP = 2, /** Stop traversal immediately. */ EXIT = 3 } /** * Visit a specific type of node. */ declare function visitNodeType<S extends string, N extends Uni.Node & { type: S; }>(tree: Uni.Node, type: S, visitor: Visitor<N>): void; export { initWikiAttrBox, fromMarkdownWikiAttrs, fromMarkdownWikiLinks, fromMarkdownWikiEmbeds, fromMarkdownWikiRefs, toMarkdownWikiAttrs, toMarkdownWikiLinks, toMarkdownWikiEmbeds, toMarkdownWikiRefs, visitNodeType, AttrBoxDataNode, AttrBoxNode, AttrBoxTitleNode, AttrBoxListNode, AttrKeyNode, AttrValNode, WikiAttrNode, WikiLinkNode, WikiEmbedNode, EmbedMkdnWrapperNode, EmbedMkdnTitleNode, EmbedMkdnLinkNode, EmbedMkdnContentNode, EmbedMediaSpanNode, EmbedMediaAudioNode, EmbedMediaImageNode, EmbedMediaVideoNode }; //# sourceMappingURL=index.d.ts.map