Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extra json escaping #7

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/md4w.zig
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ const JOSNRenderer = struct {
if (code.lang.size > 0) {
w.writeJSONProps();
w.write("\"lang\":\"");
w.writeJSONString(@as([*]const u8, @ptrCast(code.lang.text))[0..code.lang.size], 1);
w.writeJSONString(@as([*]const u8, @ptrCast(code.lang.text))[0..code.lang.size], 0);
w.write("\"}");
}
w.writeJSONChildren();
Expand Down Expand Up @@ -600,7 +600,7 @@ const JOSNRenderer = struct {
w.writeJSONString(@as([*]const u8, @ptrCast(a.href.text))[0..a.href.size], 2);
if (a.title.size > 0) {
w.write("\",\"title\":");
w.writeJSONString(@as([*]const u8, @ptrCast(a.title.text))[0..a.title.size], 1);
w.writeJSONString(@as([*]const u8, @ptrCast(a.title.text))[0..a.title.size], 0);
}
w.write("\"}");
w.writeJSONChildren();
Expand Down Expand Up @@ -647,7 +647,7 @@ const JOSNRenderer = struct {
}
if (img.title.size > 0) {
w.write("\"title\":\"");
w.writeJSONString(@as([*]const u8, @ptrCast(img.title.text))[0..img.title.size], 1);
w.writeJSONString(@as([*]const u8, @ptrCast(img.title.text))[0..img.title.size], 0);
w.write("\"");
}
w.write("}},");
Expand Down Expand Up @@ -701,9 +701,8 @@ const JOSNRenderer = struct {
}
},
else => {
const escape: u2 = if (typ == c.MD_TEXT_CODE) 0 else 1;
w.writeByte('"');
w.writeJSONString(@as([*]const u8, @ptrCast(ptr))[0..len], escape);
w.writeJSONString(@as([*]const u8, @ptrCast(ptr))[0..len], 0);
w.write("\",");
},
}
Expand Down
22 changes: 18 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ await init();

Deno.test("render to string", async (t) => {
await t.step("commonmark-spec", async () => {
const md = await Deno.readFile(
const specMd = await Deno.readFile(
new URL("commonmark-spec.md", import.meta.url),
);
const html = mdToHtml(md);
const html = mdToHtml(specMd);
assertIncludes(html, "<h1>Introduction"); // main heading
assertIncludes(html, '<ol start="2"');
assertIncludes(html, "<p>After we're done"); // last paragraph
Expand Down Expand Up @@ -149,7 +149,7 @@ Deno.test("using code hightlighter", async () => {
});

Deno.test("render to json", async () => {
const md = `
const simpleMd = `
# Jobs
Stay _foolish_, stay **hungry**!

Expand Down Expand Up @@ -185,9 +185,11 @@ console.log('Hello, world!');
| :--- | ---: |
| \`git status\` | List all *new or modified* files |
| \`git diff\` | Show file differences that **haven't been** staged |

This is hardly a "corner case," for some reason.
`;

const tree = mdToJSON(md);
const tree = mdToJSON(simpleMd);
assertEquals(tree, {
children: [
{ type: NodeType.H1, children: ["Jobs"] },
Expand Down Expand Up @@ -467,6 +469,18 @@ console.log('Hello, world!');
},
],
},
{
type: NodeType.P,
children: [
"This is hardly a \"corner case,\" for some reason.",
],
}
],
});

// Make sure spec can be parsed
// const specMd = await Deno.readFile(
// new URL("commonmark-spec.md", import.meta.url),
// );
// mdToJSON(specMd);
});