Skip to content

Commit

Permalink
🥼 Improve suffix citation parsing (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Nov 16, 2023
1 parent ac9faab commit c242d9b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-peaches-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-it-myst': patch
---

Improve the suffix label parsing for citations
29 changes: 10 additions & 19 deletions packages/markdown-it-myst/src/citations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export interface Citation {
export const citationsPlugin: PluginWithOptions = (md) => {
const regexes = {
citation: /^([^^-]|[^^].+?)?(-)?@([\w][\w:.#$%&\-+?<>~/]*)(.+)?$/,
inText: /^@((?:[\w|{][\w:.#$%&\-+?<>~/]*[\w|}])|\w)(\s*)(\[)?/,
// Only allow a short [suffix] for in text citations (e.g. 50 characters)
inText: /^@((?:[\w|{][\w:.#$%&\-+?<>~/]*[\w|}])|\w)(\s*)(\[([^\]]{1,50})\])?/,
allowedBefore: /^[^a-zA-Z.0-9]$/,
};

Expand All @@ -50,24 +51,14 @@ export const citationsPlugin: PluginWithOptions = (md) => {
(token as any).col = [state.pos];
}
if (match[3]) {
// suffix is there
const suffixStart = state.pos + match[0].length;
const suffixEnd = state.md.helpers.parseLinkLabel(state, suffixStart);
const charAfter = state.src.codePointAt(suffixEnd + 1);
if (suffixEnd > 0 && charAfter != 0x28 && charAfter != 0x5b /* ( or [ */) {
const suffix = state.src.slice(suffixStart, suffixEnd);
citation.suffix = state.md.parseInline(suffix, state.env);
state.pos += match[0].length + suffixEnd - suffixStart + 1;
if (token) {
token.content = match[0] + suffix + ']';
(token as any).col.push(state.pos);
}
} else {
state.pos += match[0].length - match[2].length - match[3].length;
if (token) {
token.content = match[0];
(token as any).col.push(state.pos);
}
// The in-text citation is followed by [suffix]
// Another way to do this is to use `state.md.helpers.parseLinkLabel(state, suffixStart);`
const suffix = match[4];
citation.suffix = state.md.parseInline(suffix, state.env);
state.pos += match[0].length;
if (token) {
token.content = match[0];
(token as any).col.push(state.pos);
}
} else {
state.pos += match[0].length - match[2].length;
Expand Down
25 changes: 25 additions & 0 deletions packages/markdown-it-myst/tests/citations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,28 @@ cases:
- type: text
content: ', the authors...'
- type: paragraph_close
- title: Nested labels
md: 'These include experimentally produced alkaline magmas from @iacovino2016 [`alkaline.xlsx`], basaltic melt inclusions from Kilauea [@tucker2019] and Gakkel Ridge [@bennett2019 `basalts.xlsx`], basaltic melt inclusions from Cerro Negro volcano, Nicaragua [@roggensack2001 `cerro_negro.xlsx`], and rhyolite melt inclusions from the Taupo Volcanic Center, New Zealand [@myers2019] and a topaz rhyolite from the Rio Grande Rift @mercer2015 [`rhyolites.xlsx`].'
tokens:
- type: paragraph_open
- type: inline
children:
- type: text
content: 'These include experimentally produced alkaline magmas from '
- type: cite
content: '@iacovino2016 [`alkaline.xlsx`]'
meta:
label: iacovino2016
kind: narrative
suffix:
- content: '`alkaline.xlsx`'
- type: text
content: ', basaltic melt inclusions from Kilauea '
- type: cite
content: '@tucker2019'
meta:
label: tucker2019
kind: parenthetical
- type: text
content: ' and Gakkel Ridge '
- type: paragraph_close

0 comments on commit c242d9b

Please sign in to comment.