Skip to content

Commit

Permalink
Patch ordered list numbering issue (#7)
Browse files Browse the repository at this point in the history
This is a dirty and quick fix. It doesn't ensure that nested lists work the same way. But all current tests pass.
  • Loading branch information
thetarnav committed Nov 7, 2024
1 parent 588f5d4 commit befc05d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
16 changes: 8 additions & 8 deletions smd.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function end_tokens_to_len(p, len) {
/**
* @param {Parser } p
* @param {Token } list_token
* @returns {void } */
* @returns {boolean} added a new list */
function continue_or_add_list(p, list_token) {
/* will create a new list inside the last item
if the amount of spaces is greater than the last one (with prefix)
Expand Down Expand Up @@ -289,13 +289,14 @@ function continue_or_add_list(p, list_token) {
if (list_idx === -1) {
end_tokens_to_len(p, p.blockquote_idx)
add_token(p, list_token)
} else {
end_tokens_to_len(p, list_idx)
return true
}
} else {
end_tokens_to_len(p, item_idx)
add_token(p, list_token)
end_tokens_to_len(p, list_idx)
return false
}
end_tokens_to_len(p, item_idx)
add_token(p, list_token)
return true
}

/**
Expand Down Expand Up @@ -533,8 +534,7 @@ export function parser_write(p, chunk) {
if ('.' === p.pending[p.pending.length-1]) {
if (' ' !== char) break // fail

continue_or_add_list(p, LIST_ORDERED)
if (p.pending !== "1.") {
if (continue_or_add_list(p, LIST_ORDERED) && p.pending !== "1.") {
p.renderer.set_attr(p.renderer.data, START, p.pending.slice(0, -1))
}
add_list_item(p, p.pending.length+1)
Expand Down
48 changes: 45 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,38 @@ function compare_child(actual, expected, lines, len) {
return false
}

if (JSON.stringify(actual.attrs) !== JSON.stringify(expected.attrs)) {
compare_push_text(JSON.stringify(actual.attrs), lines, len + 1, +1)
compare_push_text(JSON.stringify(expected.attrs), lines, len + 1, -1)
let attrs_str_actual = attrs_to_string(actual.attrs)
let attrs_str_expected = attrs_to_string(expected.attrs)
if (attrs_str_actual !== attrs_str_expected) {
compare_push_text(attrs_str_actual, lines, len + 1, +1)
compare_push_text(attrs_str_expected, lines, len + 1, -1)
return false
}

return compare_children(actual.children, expected.children, lines, len + 1)
}

/**
@param {Node_Attrs|undefined} attrs
@returns {string} */
function attrs_to_string(attrs) {
let txt = '('
if (attrs) {
let entries = /** @type {[string, string][]} */(Object.entries(attrs))
for (let i = 0; i < entries.length; i++) {
let [key, value] = entries[i]
txt += smd.attr_to_html_attr(/** @type {*} */(+key))
txt += '='
txt += value
if (i < entries.length-1) {
txt += ', '
}
}
}
txt += ')'
return txt
}

/**
* @param {Children} children
* @param {Children} expected_children
Expand Down Expand Up @@ -1132,6 +1155,25 @@ test_single_write("Blockquote with code and line break",
}]
)

test_single_write("Ordered list",
"1. hello\n"+
"2. world\n"+
"3. wave",
[{
type : smd.Token.List_Ordered,
children: [{
type : smd.Token.List_Item,
children: ["hello"],
}, {
type : smd.Token.List_Item,
children: ["world"],
}, {
type : smd.Token.List_Item,
children: ["wave"],
}],
}]
)

for (const [c, token] of /** @type {const} */([
["*", smd.Token.List_Unordered],
["-", smd.Token.List_Unordered],
Expand Down

0 comments on commit befc05d

Please sign in to comment.