From f2d0ad999e547a478c23dc38a8f6f484541cdbd9 Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Wed, 21 Dec 2016 14:04:56 +0900 Subject: [PATCH] rndr_header_anchor: use djb2 hash for non-ascii text --- ext/redcarpet/html.c | 9 +++++++++ test/html_render_test.rb | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ext/redcarpet/html.c b/ext/redcarpet/html.c index 2f717ba6..9eab059d 100644 --- a/ext/redcarpet/html.c +++ b/ext/redcarpet/html.c @@ -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 diff --git a/test/html_render_test.rb b/test/html_render_test.rb index 9bc3da95..3fdd2ca5 100644 --- a/test/html_render_test.rb +++ b/test/html_render_test.rb @@ -254,7 +254,7 @@ def test_non_ascii_removal_in_header_anchors def test_utf8_only_header_anchors markdown = "# 見出し" - html = "

見出し

" + html = "

見出し

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