Skip to content

Commit

Permalink
paramaterize parser for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cloverich committed Dec 3, 2024
1 parent 9e0733b commit a3ef665
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/markdown/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it } from "mocha";
import path from "path";

import { slateToString, stringToSlate } from "./index.js";
import { dig, parseMarkdown } from "./test-utils.js";
import { dig, parseMarkdown, parseMarkdownForImport } from "./test-utils.js";

// Tests can structure the data this way and use runTests to
// test the various conversions.
Expand Down Expand Up @@ -41,7 +41,7 @@ function outputMarkdown(markdown: string | { in: string; out: string }) {
// but is sometimes configurable (ex: options -> bullet)
// - markdown (string)->mdast
// - markdown (string)->slate
function runTests(doc: TestDoc) {
function runTests(doc: TestDoc, parser = parseMarkdown) {
it("roundtrips", function () {
const result = slateToString(stringToSlate(inputMarkdown(doc.markdown)));

Expand All @@ -54,14 +54,14 @@ function runTests(doc: TestDoc) {
// round trip properly if it does not parse at all (ex: wikilinks without a handler)
if (doc.mdast) {
it("markdown->mdast", function () {
const result = parseMarkdown(inputMarkdown(doc.markdown));
const result = parser(inputMarkdown(doc.markdown));
expect(result).to.deep.equal(doc.mdast);
});
}

if (doc.slate) {
it("markdown->slate", function () {
const result = stringToSlate(outputMarkdown(doc.markdown));
const result = stringToSlate(outputMarkdown(doc.markdown), parser);
expect(result).to.deep.equal(doc.slate);
});
}
Expand Down Expand Up @@ -477,10 +477,10 @@ describe("[[Wikilinks]]", function () {
],
};

runTests(doc);
runTests(doc, parseMarkdownForImport);
});

suite("mdast-util-ofm-tag", async () => {
describe("mdast-util-ofm-tag", async () => {
const doc = {
markdown: "a #b c",
mdast: {
Expand All @@ -498,7 +498,7 @@ suite("mdast-util-ofm-tag", async () => {
},
};

runTests(doc);
runTests(doc, parseMarkdownForImport);
});

// A place to put behavior that is not yet handled correctly; so I can store test
Expand Down
6 changes: 4 additions & 2 deletions src/markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ export const mdastToString = (tree: mdast.Nodes) => {
});
};

export const stringToSlate = (input: string) => {
return mdastToSlate(unwrapImages(parseMarkdown(input)));
// parser param: support configuring for importer tests, which import and convert
// a few otherwise unsupported markdown features (tags, wikilinks)
export const stringToSlate = (input: string, parse = parseMarkdown) => {
return mdastToSlate(unwrapImages(parse(input)));
};

export const slateToString = (nodes: SlateCustom.SlateNode[]) => {
Expand Down
8 changes: 7 additions & 1 deletion src/markdown/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Root } from "mdast";

import { parseMarkdown as parseMarkdownRaw } from "./index.js";
import {
parseMarkdownForImport as parseMarkdownForImportRaw,
parseMarkdown as parseMarkdownRaw,
} from "./index.js";

// Remove the parsed position information to simplify deep equals comparisons
// There is a similar function that's an entire npm package; fuck that.
Expand All @@ -18,6 +21,9 @@ export function prunePositions(tree: any) {
export const parseMarkdown = (markdown: string): Root =>
prunePositions(parseMarkdownRaw(markdown));

export const parseMarkdownForImport = (markdown: string): Root =>
prunePositions(parseMarkdownForImportRaw(markdown));

// Like _.get but fail loud, helpful error messages
// Usage: dig(mdast, 'children.0.children.1.value')
export function dig(obj: any, path: string) {
Expand Down

0 comments on commit a3ef665

Please sign in to comment.