@benrbray/micromark-extension-cite
Version:
Micromark syntax extension for pandoc-style citations.
232 lines (231 loc) • 5.79 kB
JavaScript
function A(a = {}) {
function i() {
let n = this.getData("inlineCiteStack");
n || this.setData("inlineCiteStack", n = []), n.push({ items: [] });
}
function u(n) {
const m = this.getData("inlineCiteStack").pop(), l = "citation", s = ((m == null ? void 0 : m.items) || []).map((p) => p.key).join(" "), x = this.sliceSerialize(n);
this.tag(`<span class="${l}" data-cites="${s}">`), this.raw(x), this.tag("</span>");
}
function r(n) {
const m = this.sliceSerialize(n), l = this.getData("inlineCiteStack");
e(l).items.push({ key: m });
}
function e(n) {
return n[n.length - 1];
}
return {
enter: {
inlineCite: i
},
exit: {
inlineCite: u,
citeItemKey: r
}
};
}
function g(a) {
const i = Object.assign({
enableAltSyntax: !1,
enablePandocSyntax: !0
}, a), u = {};
return i.enablePandocSyntax && (u[91] = { tokenize: C(!1) }), i.enableAltSyntax && (u[64] = { tokenize: C(!0) }), { text: u };
}
const K = {
partial: !0,
/** If the next two characters are `-@`, run `ok`, else `nok`. */
tokenize(a, i, u) {
return r;
function r(n) {
return n !== 45 ? u(n) : (a.consume(n), e);
}
function e(n) {
return n !== 64 ? u(n) : (a.consume(n), i(n));
}
}
}, C = (a) => function(i, u, r) {
let e = {
/** helps detect empty citation keys */
nonEmptyKey: !1,
/** note that this variable is only updated when we are looking
* for a prefix->key transition, when need to know whether the
* most recently consumed character was a space. */
lastWasSpace: !1,
/** are we currently in the prefix? */
inPrefix: !1
};
return a ? m : n;
function n(t) {
return t === 91 ? (i.enter(
"inlineCite"
/* inlineCite */
), i.enter(
"inlineCiteMarker"
/* inlineCiteMarker */
), i.consume(t), i.exit(
"inlineCiteMarker"
/* inlineCiteMarker */
), s) : r(t);
}
function m(t) {
return t === 64 ? (i.enter(
"inlineCite"
/* inlineCite */
), i.enter(
"inlineCiteMarker_alt"
/* inlineCiteMarker_alt */
), i.consume(t), l) : r(t);
}
function l(t) {
return t === 91 ? (i.consume(t), i.exit(
"inlineCiteMarker_alt"
/* inlineCiteMarker_alt */
), i.enter(
"citeItem"
/* citeItem */
), S) : r(t);
}
function S(t) {
return t === 45 ? (i.enter(
"citeAuthorSuppress"
/* citeAuthorSuppress */
), i.consume(t), i.exit(
"citeAuthorSuppress"
/* citeAuthorSuppress */
), i.enter(
"citeItemKey"
/* citeItemKey */
), I) : (i.enter(
"citeItemKey"
/* citeItemKey */
), I(t));
}
function s(t) {
return e.nonEmptyKey = !1, i.enter(
"citeItem"
/* citeItem */
), t === 45 ? p(t) : t === 64 ? y(t) : (e.lastWasSpace = !1, e.inPrefix = !0, i.enter(
"citeItemPrefix"
/* citeItemPrefix */
), x(t));
}
function x(t) {
return t === 45 ? p(t) : t === 64 ? y(t) : t === 93 || t === null ? r(t) : (e.lastWasSpace = t === 32, i.consume(t), x);
}
function p(t) {
return t !== 45 ? r(t) : i.check(
// check if the next two characters are `-@`
K,
// if they are, tokenize as citeAuthorSuppress
P,
// otherwise, we're still in the prefix
c
)(t);
}
function c(t) {
return e.inPrefix || (i.enter(
"citeItemPrefix"
/* citeItemPrefix */
), e.inPrefix = !0), i.consume(t), x;
}
function P(t) {
return t !== 45 ? r(t) : (e.inPrefix && (i.exit(
"citeItemPrefix"
/* citeItemPrefix */
), e.inPrefix = !1), i.enter(
"citeAuthorSuppress"
/* citeAuthorSuppress */
), i.consume(t), i.exit(
"citeAuthorSuppress"
/* citeAuthorSuppress */
), y);
}
function y(t) {
if (t !== 64)
return r(t);
if (e.inPrefix) {
if (!e.lastWasSpace)
return r(t);
i.exit(
"citeItemPrefix"
/* citeItemPrefix */
), e.inPrefix = !1;
}
return i.enter(
"citeItemSymbol"
/* citeItemSymbol */
), i.consume(t), i.exit(
"citeItemSymbol"
/* citeItemSymbol */
), i.enter(
"citeItemKey"
/* citeItemKey */
), I;
}
function I(t) {
if (t === 93 || t == 59) {
if (!e.nonEmptyKey)
return r(t);
if (i.exit(
"citeItemKey"
/* citeItemKey */
), i.exit(
"citeItem"
/* citeItem */
), t === 93)
return h(t);
if (t === 59)
return i.enter(
"citeItemSep"
/* citeItemSep */
), i.consume(t), i.exit(
"citeItemSep"
/* citeItemSep */
), s;
}
return t === 32 || t === 44 ? e.nonEmptyKey ? (i.exit(
"citeItemKey"
/* citeItemKey */
), i.enter(
"citeItemSuffix"
/* citeItemSuffix */
), o(t)) : r(t) : t === null || t < 0 ? r(t) : (e.nonEmptyKey = !0, i.consume(t), I);
}
function o(t) {
return t === null ? r(t) : t === 93 ? (i.exit(
"citeItemSuffix"
/* citeItemSuffix */
), i.exit(
"citeItem"
/* citeItem */
), h(t)) : t === 59 ? (i.exit(
"citeItemSuffix"
/* citeItemSuffix */
), i.exit(
"citeItem"
/* citeItem */
), i.enter(
"citeItemSep"
/* citeItemSep */
), i.consume(t), i.exit(
"citeItemSep"
/* citeItemSep */
), s) : (i.consume(t), o);
}
function h(t) {
return t !== 93 ? r(t) : (i.enter(
"inlineCiteMarker"
/* inlineCiteMarker */
), i.consume(t), i.exit(
"inlineCiteMarker"
/* inlineCiteMarker */
), i.exit(
"inlineCite"
/* inlineCite */
), u);
}
};
export {
A as citeHtml,
g as citeSyntax
};