Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #16 from curvenote/feat/ref
Browse files Browse the repository at this point in the history
New reference node and inline content
  • Loading branch information
rowanc1 authored Jun 1, 2021
2 parents 67e6bf8 + 60784a3 commit 066932e
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export { Schema } from 'prosemirror-model';
export * as schemas from './schemas';
export * as Nodes from './nodes';
export * as types from './types';

export {
fromHTML,
Expand Down
18 changes: 16 additions & 2 deletions src/marks.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import { MarkSpec } from 'prosemirror-model';
import { MarkGroups } from './nodes/types';

export type LinkAttrs = {
href: string;
title: string | null;
kind: string;
};

export const link: MarkSpec = {
attrs: {
href: {},
title: { default: null },
kind: { default: 'external' },
},
inclusive: false,
parseDOM: [{
tag: 'a[href]',
getAttrs(dom: any) {
return { href: dom.getAttribute('href'), title: dom.getAttribute('title') };
return {
href: dom.getAttribute('href'),
title: dom.getAttribute('title'),
kind: dom.getAttribute('kind') || 'external',
};
},
}],
toDOM(node) { const { href, title } = node.attrs; return ['a', { href, title }, 0]; },
toDOM(node) {
const { href, title, kind } = node.attrs;
return ['a', { href, title, kind }, 0];
},
};

export const code: MarkSpec = {
Expand Down
6 changes: 4 additions & 2 deletions src/nodes/cite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NodeGroups, FormatSerialize } from './types';
const cite: NodeSpec = {
attrs: {
key: { default: null },
inline: { default: '' },
},
inline: true,
marks: '',
Expand All @@ -14,12 +15,13 @@ const cite: NodeSpec = {
getAttrs(dom: any) {
return {
key: dom.getAttribute('key') ?? dom.getAttribute('data-key') ?? dom.getAttribute('data-cite'),
inline: dom.getAttribute('inline') ?? '',
};
},
}],
toDOM(node) {
const { key } = node.attrs;
return ['cite', { key }];
const { key, inline } = node.attrs;
return ['cite', { key, inline }];
},
};

Expand Down
1 change: 1 addition & 0 deletions src/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * as Time from './time';
// Technical
export * as Cite from './cite';
export * as CiteGroup from './cite-group';
export * as Ref from './ref';
export * as Math from './math';
export * as Equation from './equation';

Expand Down
39 changes: 39 additions & 0 deletions src/nodes/ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NodeSpec } from 'prosemirror-model';
import { NodeGroups, FormatSerialize } from './types';

const ref: NodeSpec = {
attrs: {
kind: { default: null },
key: { default: null },
},
inline: true,
marks: '',
group: NodeGroups.inline,
draggable: true,
parseDOM: [{
tag: 'cite.ref',
getAttrs(dom: any) {
return {
kind: dom.getAttribute('kind'),
key: dom.getAttribute('key') ?? dom.getAttribute('data-key') ?? dom.getAttribute('data-cite') ?? dom.getAttribute('data-ref'),
};
},
// cite is also parsed, and this is higher priority
priority: 60,
}],
toDOM(node) {
const { key, kind } = node.attrs;
return ['cite', { class: 'ref', kind, key }];
},
};


export const toMarkdown: FormatSerialize = (state, node) => {
state.write(`{ref}\`${node.attrs.key}\``);
};

export const toTex: FormatSerialize = (state, node) => {
state.write(`\\ref{${node.attrs.key}}`);
};

export default ref;
9 changes: 7 additions & 2 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const presentationalNodes = {
export const citationNodes = {
cite: Nodes.Cite.default,
cite_group: Nodes.CiteGroup.default,
ref: Nodes.Ref.default,
};

export const mathNodes = {
Expand All @@ -34,6 +35,11 @@ export const reactiveDisplayNodes = {
button: Nodes.Button.default,
};

export const reactiveNodes = {
variable: Nodes.Variable.default,
...reactiveDisplayNodes,
};

export const nodes = {
// Basic markdown
doc: basic.doc,
Expand All @@ -51,8 +57,7 @@ export const nodes = {
...presentationalNodes,
...citationNodes,
...mathNodes,
variable: Nodes.Variable.default,
...reactiveDisplayNodes,
...reactiveNodes,
};

export enum nodeNames {
Expand Down
1 change: 1 addition & 0 deletions src/serialize/markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const markdownSerializer = new MarkdownSerializer({
equation: nodes.Equation.toMarkdown,
cite: nodes.Cite.toMarkdown,
cite_group: nodes.CiteGroup.toMarkdown,
ref: nodes.Ref.toMarkdown,
// Dynamic
variable: nodes.Variable.toMarkdown,
display: nodes.Display.toMarkdown,
Expand Down
1 change: 1 addition & 0 deletions src/serialize/tex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const texSerializer = new MarkdownSerializer({
time: nodes.Time.toTex,
cite: nodes.Cite.toTex,
cite_group: nodes.CiteGroup.toTex,
ref: nodes.Ref.toTex,
math: nodes.Math.toTex,
equation: nodes.Equation.toTex,
// \usepackage{framed}
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { LinkAttrs } from './marks';

export type Parser = {
new(): DOMParser;
prototype: DOMParser;
};

export type { LinkAttrs };

0 comments on commit 066932e

Please sign in to comment.