Skip to content

Commit

Permalink
Fix start number rendering of ol (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije authored Feb 19, 2024
1 parent 0441658 commit a0c7e78
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/md4w.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ const Writer = struct {
std.mem.copyForwards(u8, self.buf[self.len..], chunk);
self.len += chunk.len;
}
pub fn writeNumber(self: *Writer, number: anytype) void {
var buffer: [4]u8 = undefined;
const buf = buffer[0..];
self.write(std.fmt.bufPrintIntToSlice(buf, number, 10, .lower, .{}));
}
fn safeWriteChar(self: *Writer, ch: u8) void {
switch (ch) {
'<' => self.write("&lt;"),
Expand Down Expand Up @@ -152,9 +157,7 @@ const Writer = struct {
}
pub fn writeJSONType(self: *Writer, typ: c.MD_BLOCKTYPE) void {
self.write("{\"type\":");
var buffer: [3]u8 = undefined;
const buf = buffer[0..];
self.write(std.fmt.bufPrintIntToSlice(buf, @as(u8, @intCast(typ)), 10, .lower, .{}));
self.writeNumber(@as(u8, @intCast(typ)));
}
pub fn writeJSONChildren(self: *Writer) void {
self.write(",\"children\":[");
Expand All @@ -167,22 +170,21 @@ const Writer = struct {
self.write(",\"props\":{");
}
pub fn writeJSONString(self: *Writer, input: []const u8, escape: u2) void {
for (input, 0..) |ch, i| {
const br = ch == '\n';
if (br or (ch == '"' and (i == 0 or input[i - 1] != '\\'))) {
for (input) |ch| switch (ch) {
'"', '\\' => {
self.writeByte('\\');
}
if (br) {
self.writeByte('n');
continue;
}
switch (escape) {
self.writeByte(ch);
},
'\n' => self.write("\\n"),
'\r' => self.write("\\r"),
'\t' => self.write("\\t"),
else => switch (escape) {
0 => self.writeByte(ch),
1 => self.safeWriteChar(ch),
2 => self.safeWriteUrlChar(ch),
else => unreachable,
}
}
},
};
}
pub fn stripTrailingComma(self: *Writer) void {
if (self.buf[self.len - 1] == ',') {
Expand Down Expand Up @@ -211,7 +213,7 @@ const HTMLRenderer = struct {
const ol: *c.MD_BLOCK_OL_DETAIL = @ptrCast(@alignCast(detail));
if (ol.start > 1) {
w.write("<ol start=\"");
w.writeByte('0' + @as(u8, @intCast(ol.start)));
w.writeNumber(@as(usize, @intCast(ol.start)));
w.write("\">\n");
} else {
w.write("<ol>\n");
Expand Down Expand Up @@ -502,7 +504,7 @@ const JOSNRenderer = struct {
if (ol.start > 1) {
w.writeJSONProps();
w.write("\"start\":");
w.writeByte('0' + @as(u8, @intCast(ol.start)));
w.writeNumber(@as(usize, @intCast(ol.start)));
w.writeByte('}');
}
w.writeJSONChildren();
Expand Down Expand Up @@ -643,12 +645,14 @@ const JOSNRenderer = struct {
if (typ == c.MD_SPAN_IMG) {
const img: *c.MD_SPAN_IMG_DETAIL = @ptrCast(@alignCast(detail));
if (w.buf[w.len - 1] == ':') {
w.write("\"\""); // no alt text
w.write("\"\","); // no alt text
}
if (img.title.size > 0) {
w.write("\"title\":\"");
w.writeJSONString(@as([*]const u8, @ptrCast(img.title.text))[0..img.title.size], 1);
w.write("\"");
} else {
w.stripTrailingComma();
}
w.write("}},");
} else {
Expand Down Expand Up @@ -676,7 +680,7 @@ const JOSNRenderer = struct {
c.MD_TEXT_BR => {
if (w.image_nesting_level == 0) {
w.writeJSONTypeAndChildren(c.MD_BLOCK_HTML);
w.write("\"<br>\n\"],");
w.write("\"<br>\\n\"]},");
} else {
w.write("\" \",");
}
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,16 @@ console.log('Hello, world!');
},
],
});

const specMd = await Deno.readFile(
new URL("commonmark-spec.md", import.meta.url),
);
const tree2 = mdToJSON(specMd);
assertEquals(Array.isArray(tree2.children), true);

const readmeMd = await Deno.readFile(
new URL("../README.md", import.meta.url),
);
const tree3 = mdToJSON(readmeMd);
assertEquals(Array.isArray(tree3.children), true);
});

0 comments on commit a0c7e78

Please sign in to comment.