mdast-util-caml
Version:
Converts a `micromark` token stream into an `mdast` syntax tree.
117 lines • 3.93 kB
TypeScript
/// <reference types="unist" />
import { AttrDataPrimitive, CamlOptions, AttrData } from "micromark-extension-caml";
import * as Uni from "unist";
import { Token } from "micromark-util-types";
import { CompileContext } from "mdast-util-from-markdown";
import { Handle } 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;
}
}
// node types
// node data note:
//
// it might feel like some data properties commit data duplicatation (because they are),
// but this is intentional.
// this data provides the end-user with a more direct means of inspecting
// the shape of the attribute-wikilink data, as opposed to sifting through
// html properties (hName, hProperties, hChildren...) to figure it out.
// attr nodes
// 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: (AttrDataPrimitive | any)[]; // ...or caml's 'WikiAttrNode' (using 'any' to avoid importing wikirefs)
data: {
hName: "dd";
};
}
// caml primitives
interface PrimitiveAttrNode extends Uni.Parent {
type: string;
children: any[]; // text node: https://github.com/syntax-tree/mdast#text
data: {
// 'item' data stored in top-level 'AttrNode'
hName: "span";
hProperties: any | null;
};
}
// todo: link to micromark-extension-wikirefs instance
// this interface is to help cut down on interdependencies between caml + wikirefs
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;
};
}
declare function initAttrBox(tree: Uni.Node, opts?: Partial<CamlOptions>): AttrBoxNode | undefined;
// required options
declare function fromMarkdownCaml(this: any, opts?: Partial<CamlOptions>): {
enter: {
attrBox: (this: CompileContext, token: Token) => void;
};
exit: {
attrKey: (this: CompileContext, token: Token) => void;
attrVal: (this: CompileContext, token: Token) => void;
attrBox: (this: CompileContext, token: Token) => void;
};
};
declare function toMarkdownCaml(this: any, opts?: Partial<CamlOptions>): {
unsafe: {
character: string;
inConstruct: string[];
}[];
handlers: {
"attrbox-data": Handle;
attrboxData: Handle;
};
};
export { initAttrBox, fromMarkdownCaml, toMarkdownCaml, AttrBoxDataNode, AttrBoxNode, AttrBoxTitleNode, AttrBoxListNode, AttrKeyNode, AttrValNode, PrimitiveAttrNode, WikiAttrNode };
//# sourceMappingURL=index.d.ts.map