mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-12 11:48:29 +01:00
Compare the package with the other ADF converters in the README
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { createRequire } from "node:module";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import * as atlaskitJson from "@atlaskit/editor-json-transformer";
|
||||
import * as atlaskitMarkdown from "@atlaskit/editor-markdown-transformer";
|
||||
import * as adfCodec from "@larvit/adf-codec";
|
||||
import * as adfToMarkdown from "adf-to-markdown";
|
||||
import adfToMd from "adf-to-md";
|
||||
import * as adf2markdown from "adf2markdown";
|
||||
import * as extendedParser from "extended-markdown-adf-parser";
|
||||
import * as marklassian from "marklassian";
|
||||
import mdToAdf from "md-to-adf";
|
||||
|
||||
const { defaultSchema } = createRequire(import.meta.url)("@atlaskit/adf-schema/schema-default");
|
||||
|
||||
const roundTripDoc = {
|
||||
type: "doc", version: 1, content: [
|
||||
{ type: "paragraph", content: [
|
||||
{ type: "text", text: "bold italic", marks: [{ type: "strong" }, { type: "em" }] },
|
||||
{ type: "text", text: " and " },
|
||||
{ type: "text", text: "a link", marks: [{ type: "link", attrs: { href: "https://example.com" } }] },
|
||||
{ type: "text", text: " hi " },
|
||||
{ type: "mention", attrs: { id: "abc", text: "@Ann" } },
|
||||
{ type: "text", text: " state " },
|
||||
{ type: "status", attrs: { color: "green", localId: "s1", text: "DONE" } },
|
||||
] },
|
||||
{ type: "panel", attrs: { panelType: "info" }, content: [{ type: "paragraph", content: [{ type: "text", text: "note" }] }] },
|
||||
{ type: "bulletList", content: [
|
||||
{ type: "listItem", content: [{ type: "paragraph", content: [{ type: "text", text: "one" }] }] },
|
||||
{ type: "listItem", content: [{ type: "paragraph", content: [{ type: "text", text: "two" }] }] },
|
||||
] },
|
||||
],
|
||||
};
|
||||
const unknownNodeDoc = {
|
||||
type: "doc", version: 1, content: [
|
||||
{ type: "paragraph", content: [{ type: "text", text: "before" }] },
|
||||
{ type: "futureThing", attrs: { x: 1 }, content: [{ type: "paragraph", content: [{ type: "text", text: "inside" }] }] },
|
||||
{ type: "paragraph", content: [{ type: "text", text: "after" }] },
|
||||
],
|
||||
};
|
||||
const markdown = "***bold italic*** and [a link](https://example.com) hi @Ann\n\n> note\n\n- one\n- two\n";
|
||||
|
||||
const extended = new extendedParser.Parser();
|
||||
const json = new atlaskitJson.JSONTransformer();
|
||||
const atlaskit = new atlaskitMarkdown.MarkdownTransformer(defaultSchema);
|
||||
const converters = {
|
||||
"@atlaskit/editor-markdown-transformer": {
|
||||
toAdf: (md) => json.encode(atlaskit.parse(md)),
|
||||
toMarkdown: (doc) => atlaskit.encode(json.parse(doc)),
|
||||
},
|
||||
"@larvit/adf-codec": {
|
||||
toAdf: (md) => adfCodec.losslessMarkdownToAdf(md).value,
|
||||
toMarkdown: (doc) => adfCodec.adfToLosslessMarkdown(doc).value,
|
||||
},
|
||||
"adf-to-markdown": { toMarkdown: (doc) => adfToMarkdown.convertADFToMarkdown(doc) },
|
||||
"adf-to-md": { toMarkdown: (doc) => adfToMd.convert(doc).result },
|
||||
adf2markdown: { toMarkdown: (doc) => adf2markdown.adf2markdown(doc) },
|
||||
"extended-markdown-adf-parser": {
|
||||
toAdf: (md) => extended.markdownToAdf(md),
|
||||
toMarkdown: (doc) => extended.adfToMarkdown(doc),
|
||||
},
|
||||
marklassian: { toAdf: (md) => marklassian.markdownToAdf(md) },
|
||||
"md-to-adf": { toAdf: (md) => mdToAdf(md) },
|
||||
};
|
||||
|
||||
const show = (label, call) => {
|
||||
try {
|
||||
const value = call();
|
||||
console.log(`${label}:`, JSON.stringify(value));
|
||||
return value;
|
||||
} catch (error) {
|
||||
console.log(`${label} threw:`, error.message);
|
||||
}
|
||||
};
|
||||
for (const [name, { toAdf, toMarkdown }] of Object.entries(converters)) {
|
||||
const manifest = JSON.parse(readFileSync(`node_modules/${name}/package.json`, "utf8"));
|
||||
console.log(`\n## ${name} ${manifest.version}`);
|
||||
console.log("runtime dependencies:", Object.keys(manifest.dependencies ?? {}).length);
|
||||
const written = toMarkdown && show("ADF to markdown", () => toMarkdown(roundTripDoc));
|
||||
if (written !== undefined) {
|
||||
if (toAdf) show("round-trip deep-equal", () => isDeepStrictEqual(toAdf(written), roundTripDoc));
|
||||
const unknown = show("unknown node to markdown", () => toMarkdown(unknownNodeDoc));
|
||||
if (toAdf && unknown !== undefined) show("unknown node read back", () => toAdf(unknown));
|
||||
}
|
||||
if (toAdf) show("markdown to ADF", () => toAdf(markdown));
|
||||
}
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
# Re-checks the README's comparison table against each package's published version.
|
||||
set -eu
|
||||
here=$(cd "$(dirname "$0")" && pwd)
|
||||
docker run --rm --ulimit core=0 -u "$(id -u):$(id -g)" -e HOME=/tmp -v "$here:/comparison:ro" -w /tmp \
|
||||
node:24.21.0-alpine3.24 sh -c '
|
||||
npm install --ignore-scripts --no-audit --no-fund --silent \
|
||||
@atlaskit/editor-json-transformer@10.0.0 \
|
||||
@atlaskit/editor-markdown-transformer@6.3.1 \
|
||||
@larvit/adf-codec@0.2.0 \
|
||||
adf-to-markdown@1.0.1 \
|
||||
adf-to-md@1.2.1 \
|
||||
adf2markdown@1.2.0 \
|
||||
extended-markdown-adf-parser@2.4.0 \
|
||||
marklassian@1.2.1 \
|
||||
md-to-adf@0.6.4 &&
|
||||
cp /comparison/compare.mjs . && node compare.mjs'
|
||||
Reference in New Issue
Block a user