Skip to content

Commit

Permalink
Merge pull request #13 from pubpub/types-1.22
Browse files Browse the repository at this point in the history
Compatibility with `pandoc-types` 1.22
  • Loading branch information
idreyn authored Aug 23, 2021
2 parents 04f4b2f + 8773682 commit f820a69
Show file tree
Hide file tree
Showing 23 changed files with 3,738 additions and 1,658 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ module.exports = {
},
"rules": {
"@typescript-eslint/ban-ts-ignore": 0,
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/camelcase": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/indent": 0,
"@typescript-eslint/no-empty-interface": 0,
"@typescript-eslint/no-explicit-any": 0,
Expand Down
2,639 changes: 1,389 additions & 1,250 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"name": "@pubpub/prosemirror-pandoc",
"version": "0.6.1",
"version": "0.7.0-beta.0",
"description": "Convert between Prosemirror schemas and the Pandoc AST",
"main": "dist/index.js",
"devDependencies": {
"@types/jest": "^24.0.18",
"@types/katex": "^0.10.2",
"@types/node": "^12.7.4",
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"@typescript-eslint/eslint-plugin": "^4.29.2",
"@typescript-eslint/parser": "^4.29.2",
"eslint": "^6.0.1",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-prettier": "^3.1.0",
"esm": "^3.2.25",
"jest": "^24.9.0",
"katex": "^0.11.1",
"prettier": "^2.3.2",
Expand All @@ -27,7 +26,8 @@
"build": "tsc",
"lint": "eslint src/**/*.ts",
"prepublishOnly": "npm run lint && npm test && npm run build",
"convert": "ts-node src/example/convert.ts"
"convert": "ts-node src/example/convert.ts",
"parse": "ts-node src/example/parse.ts"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export let PANDOC_API_VERSION = [1, 20];
export let PANDOC_API_VERSION = [1, 22];

export const setPandocApiVersion = (version) => {
PANDOC_API_VERSION = version;
Expand Down
82 changes: 72 additions & 10 deletions src/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import {
Block,
BlockQuote,
BulletList,
Caption,
Cell,
CitationMode,
Cite,
Code,
CodeBlock,
ColSpec,
DefinitionList,
Div,
Doc,
Expand All @@ -27,12 +30,15 @@ import {
Note,
OrderedList,
PandocJson,
PandocNode,
Para,
Plain,
Quoted,
QuoteType,
RawBlock,
RawInline,
Row,
SimpleInline,
SmallCaps,
Span,
Str,
Expand All @@ -41,8 +47,10 @@ import {
Subscript,
Superscript,
Table,
TableBody,
TableFoot,
TableHead,
Target,
PandocNode,
} from "./types";

const wrapEnum = <T>(instance: T): { t: T } => {
Expand Down Expand Up @@ -88,9 +96,7 @@ const emitStr = (str: Str) => {
};
};

const emitSimpleInline = (
node: Emph | Strong | Strikeout | Superscript | Subscript | SmallCaps
) => {
const emitSimpleInline = (node: SimpleInline) => {
const { type, content } = node;
return {
t: type,
Expand Down Expand Up @@ -196,6 +202,7 @@ export const emitInline = (n: Inline): { t: string; c?: string | any[] } => {
return emitStr(n);
case "Emph":
case "Strong":
case "Underline":
case "Strikeout":
case "Superscript":
case "Subscript":
Expand Down Expand Up @@ -325,16 +332,71 @@ const emitDiv = (div: Div) => {
};
};

const emitCell = (cell: Cell) => {
const { attr, alignment, rowSpan, colSpan, content } = cell;
return [
wrapAttr(attr),
wrapEnum(alignment),
rowSpan,
colSpan,
content.map(emitBlock),
];
};

const emitRow = (row: Row) => {
const { attr, cells } = row;
return [wrapAttr(attr), cells.map(emitCell)];
};

const emitTableHead = (head: TableHead) => {
const { attr, rows } = head;
return [wrapAttr(attr), rows.map(emitRow)];
};

const emitTableFoot = (foot: TableFoot) => {
const { attr, rows } = foot;
return [wrapAttr(attr), rows.map(emitRow)];
};

const emitTableBody = (body: TableBody) => {
const { attr, rowHeadColumns, headRows, bodyRows } = body;
return [
wrapAttr(attr),
rowHeadColumns,
headRows.map(emitRow),
bodyRows.map(emitRow),
];
};

const emitColSpec = (colSpec: ColSpec) => {
const { alignment } = colSpec;
return [
wrapEnum<Alignment>(alignment),
"defaultWidth" in colSpec
? { t: "ColWidthDefault" }
: { t: "ColWidth", c: colSpec.width },
];
};

const emitCaption = (caption: Caption) => {
const { shortCaption, content } = caption;
return [
shortCaption ? shortCaption.map(emitInline) : null,
content.map(emitBlock),
];
};

const emitTable = (table: Table) => {
const { caption, alignments, columnWidths, headers, cells } = table;
const { attr, caption, colSpecs, head, bodies, foot } = table;
return {
t: "Table",
c: [
caption.map(emitInline),
alignments.map((alignment) => wrapEnum<Alignment>(alignment)),
columnWidths,
headers.map((blocks) => blocks.map(emitBlock)),
cells.map((row) => row.map((cell) => cell.map(emitBlock))),
wrapAttr(attr),
emitCaption(caption),
colSpecs.map(emitColSpec),
emitTableHead(head),
bodies.map(emitTableBody),
emitTableFoot(foot),
],
};
};
Expand Down
4 changes: 3 additions & 1 deletion src/example/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const main = async () => {
const {
_: [filePath],
} = argv;
console.log(JSON.stringify(loadAndTransformFromPandoc(filePath, rules)));
console.log(
JSON.stringify(loadAndTransformFromPandoc(filePath as string, rules))
);
};

main().catch((e) => console.error(e));
14 changes: 14 additions & 0 deletions src/example/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs from "fs";
import { argv } from "yargs";
import { parsePandocJson } from "../parse";

const main = async () => {
const {
_: [filePath],
} = argv;
const fileJson = JSON.parse(fs.readFileSync(filePath).toString());
const parsed = parsePandocJson(fileJson);
console.log(JSON.stringify(parsed));
};

main().catch((e) => console.error(e));
21 changes: 9 additions & 12 deletions src/example/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import * as katex from "katex";

import { nodes, marks } from "./schema";
import {
PandocNode,
Image,
Str,
Space,
Header,
LineBlock,
ProsemirrorNode,
Expand Down Expand Up @@ -36,11 +33,10 @@ import {
listTransformer,
nullTransformer,
pandocPassThroughTransformer,
tableTransformer,
pandocQuotedTransformer,
} from "../transform/commonTransformers";

import { buildRuleset, BuildRuleset } from "../transform/transformer";
pandocTableTransformer,
} from "../transform/transformers";
import { buildRuleset } from "../transform/transformer";

import {
pandocInlineToHtmlString,
Expand All @@ -49,7 +45,7 @@ import {
htmlStringToPandocBlocks,
} from "./util";

const rules: BuildRuleset<PandocNode, ProsemirrorNode> = buildRuleset({
const rules = buildRuleset({
nodes,
marks,
});
Expand Down Expand Up @@ -133,7 +129,7 @@ rules.fromPandoc(

// Tranform headers
rules.transform("Header", "heading", {
fromPandoc: (node: Header, { transform }) => {
fromPandoc: (node, { transform }) => {
return {
type: "heading",
attrs: {
Expand Down Expand Up @@ -182,7 +178,7 @@ rules.transformToMark("Link", "link", (link: Link) => {
rules.fromPandoc("SmallCaps", pandocPassThroughTransformer);

// Tell the transformer how to deal with typical content-level nodes
rules.fromPandoc("(Str | Space)+", (nodes: (Str | Space)[]) => {
rules.fromPandoc("(Str | Space)+", (nodes) => {
return {
type: "text",
text: textFromStrSpace(nodes),
Expand All @@ -200,6 +196,7 @@ rules.fromPandoc("SoftBreak", nullTransformer);

// Stuff we don't have equivalents for
rules.fromPandoc("Span", pandocPassThroughTransformer);
rules.fromPandoc("Underline", pandocPassThroughTransformer);

// Anything in quotation marks is its own node, to Pandoc
rules.fromPandoc("Quoted", pandocQuotedTransformer);
Expand Down Expand Up @@ -229,7 +226,7 @@ rules.fromPandoc("RawInline", (node: RawInline) => {
});

// Tables
rules.transform("Table", "table", tableTransformer);
rules.fromPandoc("Table", pandocTableTransformer);

// Equations
rules.fromPandoc("Math", (node: Math) => {
Expand Down Expand Up @@ -298,7 +295,7 @@ rules.transform("Cite", "citation", {
const citationNumber =
typeof node.attrs.count === "number"
? node.attrs.count
: parseInt(node.attrs.count);
: parseInt(node.attrs.count as string);
return {
type: "Cite",
content: htmlStringToPandocInline(inputHtml),
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as transformUtil from "./transform/util";
import * as commonTransformers from "./transform/commonTransformers";
import * as transformers from "./transform/transformers";

export { transformUtil, commonTransformers };
console.log("greetings");

export { transformUtil, transformers };
export { fromPandoc } from "./transform/fromPandoc";
export { buildRuleset } from "./transform/transformer";
export { emitPandocJson } from "./emit";
Expand Down
Loading

0 comments on commit f820a69

Please sign in to comment.