Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥼 Improve suffix citation parsing #757

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})\])?/,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this regex includes the suffix, while the previous did not. That means we are no longer using parseLinkLabel and instead just get the suffix up front as raw text. Probably ok? Certainly makes the code simpler!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the parse link label was taking a lot of content (including other citations and links) and I think we want to be pretty targeted here and ensure that we can close the suffix label properly!

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`]'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running this test before your change - this content was enormous! Now it looks nice. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

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