Skip to content

Commit

Permalink
rndr_header_anchor: use djb2 hash for non-ascii text
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Dec 21, 2016
1 parent 8d8e1ec commit f2d0ad9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ext/redcarpet/html.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ rndr_header_anchor(struct buf *out, const struct buf *anchor)
// replace the last dash if there was anything added
if (stripped && inserted)
out->size--;

// if anchor found empty, use djb2 hash for it
if (!inserted && anchor->size) {
unsigned long hash = 5381;
for (i = 0; i < size; ++i) {
hash = ((hash << 5) + hash) + a[i]; /* h * 33 + c */
}
bufprintf(out, "part-%lx", hash);
}
}

static void
Expand Down
2 changes: 1 addition & 1 deletion test/html_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def test_non_ascii_removal_in_header_anchors

def test_utf8_only_header_anchors
markdown = "# 見出し"
html = "<h1 id=\"\">見出し</h1>"
html = "<h1 id=\"part-37870bfa194139f\">見出し</h1>"

assert_equal html, render(markdown, with: [:with_toc_data])
end
Expand Down

0 comments on commit f2d0ad9

Please sign in to comment.