Skip to content

Commit

Permalink
πŸ§‘πŸΌβ€πŸ« Support additional citation roles
Browse files Browse the repository at this point in the history
See #82
  • Loading branch information
rowanc1 committed Sep 26, 2023
1 parent 43defe2 commit 20b9a41
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-snails-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-roles': patch
---

Support additional citation roles
48 changes: 39 additions & 9 deletions packages/myst-roles/src/cite.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,59 @@
import type { CiteKind } from 'myst-spec-ext';
import type { RoleSpec, RoleData, GenericNode } from 'myst-common';
import { normalizeLabel, ParseTypesEnum } from 'myst-common';
import type { Cite, CiteGroup, CiteKind } from 'myst-spec-ext';
import type { RoleSpec, RoleData } from 'myst-common';
import { normalizeLabel } from 'myst-common';

export const citeRole: RoleSpec = {
name: 'cite:p',
alias: ['cite:t', 'cite'],
name: 'cite',
alias: [
'cite:p',
'cite:t',
// https://sphinxcontrib-bibtex.readthedocs.io/en/latest/usage.html
'cite:ps',
'cite:ts',
'cite:ct',
'cite:cts',
'cite:alp',
'cite:alps',
'cite:label',
'cite:labelpar',
'cite:year',
'cite:yearpar',
'cite:author',
'cite:authors',
'cite:authorpar',
'cite:authorpars',
'cite:cauthor',
'cite:cauthors',
// 'cite:empty',
],
body: {
type: ParseTypesEnum.string,
type: String,
required: true,
},
run(data: RoleData): GenericNode[] {
run(data: RoleData): (Cite | CiteGroup)[] {
const content = data.body as string;
const labels = content.split(/[,;]/).map((s) => s.trim());
const kind: CiteKind =
data.name.startsWith('cite:p') || data.name.includes('par') ? 'parenthetical' : 'narrative';
const children = labels.map((l) => {
const { label, identifier } = normalizeLabel(l) ?? {};
return {
const cite: Cite = {
type: 'cite',
kind,
label: label ?? l,
identifier,
};
if (data.name.startsWith('cite:year')) {
cite.partial = 'year';
}
if (data.name.startsWith('cite:author') || data.name.startsWith('cite:cauthor')) {
cite.partial = 'author';
}
return cite;
});
if (data.name === 'cite' && children.length === 1) {
return children;
}
const kind: CiteKind = data.name === 'cite:p' ? 'parenthetical' : 'narrative';
return [
{
type: 'citeGroup',
Expand Down

0 comments on commit 20b9a41

Please sign in to comment.