@benrbray/mdast-util-cite
Version:
Converts a `micromark` token stream into an `mdast` syntax tree.
93 lines (92 loc) • 2.73 kB
JavaScript
import "mdast-util-to-hast";
function i(e) {
return e[e.length - 1];
}
const C = function(e) {
this.enter({
type: "cite",
// @ts-ignore: create invalid citeItem, to be filled later
value: null,
data: {
citeItems: [],
hName: "span",
hProperties: {
className: ["cite-inline"]
},
hChildren: []
}
}, e);
}, N = function(e) {
let t = i(this.stack);
this.exit(e);
const n = this.sliceSerialize(e);
t.value = n, t.data.hChildren.push({ type: "text", value: n }), t.data.hProperties["data-cite"] = JSON.stringify(t.data.citeItems);
}, $ = function(e) {
const t = i(this.stack);
t.data.altSyntax = !0;
}, _ = function(e) {
i(this.stack).data.citeItems.push({});
}, A = function(e) {
const t = i(this.stack);
i(t.data.citeItems), this.sliceSerialize(e);
}, z = function(e) {
const t = i(this.stack), n = i(t.data.citeItems);
n.suppressAuthor = !0;
}, K = function(e) {
const t = i(this.stack), n = i(t.data.citeItems), s = this.sliceSerialize(e);
n.key = s;
}, g = function(e) {
const t = i(this.stack), n = i(t.data.citeItems), s = this.sliceSerialize(e);
n.suffix = s;
}, v = function(e) {
const t = i(this.stack), n = i(t.data.citeItems), s = this.sliceSerialize(e);
n.prefix = s;
}, M = {
enter: {
inlineCite: C,
citeItem: _
},
exit: {
inlineCite: N,
inlineCiteMarker_alt: $,
citeItem: A,
citeItemPrefix: v,
citeAuthorSuppress: z,
citeItemKey: K,
citeItemSuffix: g
}
};
function b(e = {}) {
const t = Object.assign({
standardizeAltSyntax: !1,
enableAuthorSuppression: !0,
useNodeValue: !1
}, e), n = [
{ character: ",", inConstruct: ["citationKey"] },
{ character: "@", inConstruct: ["citation"] }
], s = function(c, m, a) {
return c.value;
}, p = function(c, m, a) {
if (c.data.citeItems.length === 0)
return "";
const f = c.data.citeItems[0], d = !t.standardizeAltSyntax && c.data.altSyntax === !0 && (f.prefix === void 0 || f.prefix === ""), S = a.enter("citation"), y = c.data.citeItems.map((r, h) => {
const k = a.enter("citationKey"), o = r.key;
k();
const x = r.prefix && (!d || h > 0) ? r.prefix : "", u = r.suffix || "", l = t.enableAuthorSuppression && r.suppressAuthor === !0 ? "-" : "";
return h === 0 ? d ? `@[${l}${o}${u}` : `[${x}${l}@${o}${u}` : `;${x}${l}@${o}${u}`;
});
return S(), y.join("") + "]";
}, I = t.useNodeValue ? s : p;
return {
unsafe: n,
handlers: {
// as of (2021-05-07), the typings for Handle do not reflect
// that the handler will be passed nodes of a specific type
cite: I
}
};
}
export {
M as citeFromMarkdown,
b as citeToMarkdown
};