Skip to content

Commit

Permalink
πŸ‘©β€πŸ”¬ Add cite nodes to myst-spec-ext (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Feb 23, 2023
1 parent efe5519 commit 9fcf25a
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 25 deletions.
9 changes: 9 additions & 0 deletions .changeset/serious-ducks-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'myst-cli': patch
'myst-roles': patch
'myst-spec-ext': patch
'myst-to-docx': patch
'tex-to-myst': patch
---

Add citation nodes to myst-spec-ext
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/myst-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"myst-frontmatter": "^0.0.9",
"myst-parser": "^0.0.20",
"myst-spec": "^0.0.4",
"myst-spec-ext": "^0.0.6",
"myst-templates": "^0.1.10",
"myst-to-docx": "^0.0.15",
"myst-to-tex": "^0.0.18",
Expand Down
17 changes: 1 addition & 16 deletions packages/myst-cli/src/transforms/citations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,10 @@ import { InlineCite } from 'citation-js-utils';
import type { Logger } from 'myst-cli-utils';
import type { References } from 'myst-common';
import type { StaticPhrasingContent, Parent } from 'myst-spec';
import type { Cite, CiteKind, CiteGroup } from 'myst-spec-ext';
import { selectAll } from 'unist-util-select';
import type { Root } from 'mdast';

export type CiteKind = 'narrative' | 'parenthetical';

export type Cite = {
type: 'cite';
kind: CiteKind;
label: string;
children: StaticPhrasingContent[];
error?: boolean;
};

export type CiteGroup = {
type: 'citeGroup';
kind: CiteKind;
children: Cite[];
};

function pushCite(
references: Pick<References, 'cite'>,
citeRenderer: CitationRenderer,
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-cli/src/transforms/dois.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { tic } from 'myst-cli-utils';
import type { Logger } from 'myst-cli-utils';
import type { Root } from 'mdast';
import { toText } from 'myst-common';
import type { Cite } from './citations';
import type { Cite } from 'myst-spec-ext';
import type { SingleCitationRenderer } from './types';

async function getDoiOrgBibtex(log: Logger, doiString: string): Promise<string | null> {
Expand Down
3 changes: 2 additions & 1 deletion packages/myst-roles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"url": "https://github.com/executablebooks/mystjs/issues"
},
"dependencies": {
"myst-common": "^0.0.14"
"myst-common": "^0.0.14",
"myst-spec-ext": "^0.0.6"
},
"devDependencies": {
"@types/jest": "^28.1.6",
Expand Down
4 changes: 3 additions & 1 deletion packages/myst-roles/src/cite.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CiteKind } from 'myst-spec-ext';
import type { RoleSpec, RoleData, GenericNode } from 'myst-common';
import { normalizeLabel, ParseTypesEnum } from 'myst-common';

Expand All @@ -22,10 +23,11 @@ export const citeRole: RoleSpec = {
if (data.name === 'cite' && children.length === 1) {
return children;
}
const kind: CiteKind = data.name === 'cite:p' ? 'parenthetical' : 'narrative';
return [
{
type: 'citeGroup',
kind: data.name === 'cite:p' ? 'parenthetical' : 'narrative',
kind,
children,
},
];
Expand Down
17 changes: 17 additions & 0 deletions packages/myst-spec-ext/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
Parent,
StaticPhrasingContent,
TableCell as SpecTableCell,
FootnoteReference as FNR,
FootnoteDefinition as FND,
Expand Down Expand Up @@ -61,3 +62,19 @@ export type Image = SpecImage & {
export type Admonition = SpecAdmonition & {
icon?: boolean;
};

export type CiteKind = 'narrative' | 'parenthetical';

export type Cite = {
type: 'cite';
kind: CiteKind;
label: string;
children: StaticPhrasingContent[];
error?: boolean;
};

export type CiteGroup = {
type: 'citeGroup';
kind: CiteKind;
children: Cite[];
};
6 changes: 2 additions & 4 deletions packages/myst-to-docx/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import type {
CaptionNumber,
FootnoteReference,
FootnoteDefinition,
CiteKind,
} from 'myst-spec-ext';
import type { Handler, Mutable } from './types';
import {
Expand Down Expand Up @@ -489,10 +490,7 @@ const cite: Handler<{ type: 'cite' } & Parent> = (state, node) => {
state.renderChildren(node);
};

const citeGroup: Handler<{ type: 'citeGroup'; kind: 'narrative' | 'parenthetical' } & Parent> = (
state,
node,
) => {
const citeGroup: Handler<{ type: 'citeGroup'; kind: CiteKind } & Parent> = (state, node) => {
if (node.kind === 'narrative') {
state.renderChildren(node);
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/tex-to-myst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@unified-latex/unified-latex": "^1.2.2",
"myst-common": "^0.0.14",
"myst-frontmatter": "^0.0.9",
"myst-spec-ext": "^0.0.6",
"unist-util-select": "^4.0.0",
"vfile-reporter": "^7.0.4"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/tex-to-myst/src/citations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Handler, ITexParser } from './types';
import { u } from 'unist-builder';
import { getArguments, replaceTextValue, texToText } from './utils';
import type { CiteKind } from 'myst-spec-ext';
import type { GenericNode } from 'myst-common';

function createCitation(
state: ITexParser,
node: GenericNode,
kind?: 'parenthetical' | 'narrative',
kind?: CiteKind,
partial?: 'author' | 'year' | 'number',
) {
state.openParagraph();
Expand Down

0 comments on commit 9fcf25a

Please sign in to comment.