From 9276a14c41e95e5ca17c648fe71c162f35551057 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Wed, 11 May 2022 18:09:16 +0300 Subject: Add `id` attributes to headings based on value. --- resources/test/core_result.html | 8 +++---- src/clarktown/renderers/heading_block.clj | 28 +++++++++++++++++++------ test/clarktown/renderers/heading_block_test.clj | 26 +++++++++++------------ 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/resources/test/core_result.html b/resources/test/core_result.html index 54273e0..01f91aa 100644 --- a/resources/test/core_result.html +++ b/resources/test/core_result.html @@ -48,7 +48,7 @@ function markdownToHTML(markdown) {

This is bold italic text and this is also. What about italic text that has bold text?

-

Hi there, world!

+

Hi there, world!

@@ -59,14 +59,14 @@ function markdownToHTML(markdown) {
code goes here.
 # This is a heading.
-

This is a H1 heading with settext

+

This is a H1 heading with settext

-

And this is a H2 heading with settext

+

And this is a H2 heading with settext

Testing paragraph right before a code block

code goes here
-

Heading goes here

+

Heading goes here

Paragraph right after heading

\ No newline at end of file diff --git a/src/clarktown/renderers/heading_block.clj b/src/clarktown/renderers/heading_block.clj index 7f4c7de..563508e 100644 --- a/src/clarktown/renderers/heading_block.clj +++ b/src/clarktown/renderers/heading_block.clj @@ -1,9 +1,23 @@ (ns clarktown.renderers.heading-block (:require [clojure.string :as string] - [clarktown.matchers.heading-block :as matcher])) + [clarktown.matchers.heading-block :as matcher]) + (:import + (java.text Normalizer Normalizer$Form))) +(defn- slugify + "Turn `input` in a URL slug." + [input] + (let [split-s(-> (Normalizer/normalize input Normalizer$Form/NFD) + (string/replace #"[\P{ASCII}]+" "") + string/lower-case + string/triml + (string/split #"[\p{Space}\p{P}]+")) + combined (string/join "-" split-s)] + (apply str (take 250 combined)))) + + (defn render-atx-heading "Renders the hashbang heading block." [block] @@ -15,8 +29,9 @@ value (->> (string/split single-line-block #" ") next (string/join " ") - string/trim)] - (str "" value ""))) + string/trim) + id (slugify value)] + (str "" value ""))) (defn render-settext-heading @@ -29,10 +44,11 @@ h1? (= "=" (-> (last lines) string/trim (string/split #"") - first))] + first)) + id (slugify value)] (if h1? - (str "

" value "

") - (str "

" value "

")))) + (str "

" value "

") + (str "

" value "

")))) (defn render diff --git a/test/clarktown/renderers/heading_block_test.clj b/test/clarktown/renderers/heading_block_test.clj index 8f229fb..a0852e8 100644 --- a/test/clarktown/renderers/heading_block_test.clj +++ b/test/clarktown/renderers/heading_block_test.clj @@ -6,27 +6,27 @@ (deftest atx-heading-renderer-test (testing "ATX heading block that's a H1" - (is (= "

This is a heading block.

" + (is (= "

This is a heading block.

" (heading-block/render "# This is a heading block." nil nil)))) (testing "ATX heading block that's a H2" - (is (= "

This is a heading block.

" + (is (= "

This is a heading block.

" (heading-block/render "## This is a heading block." nil nil)))) (testing "ATX heading block that's a H3" - (is (= "

This is a heading block.

" + (is (= "

This is a heading block.

" (heading-block/render "### This is a heading block." nil nil)))) (testing "ATX heading block that's a H4" - (is (= "

This is a heading block.

" + (is (= "

This is a heading block.

" (heading-block/render "#### This is a heading block." nil nil)))) (testing "ATX heading block that's a H5" - (is (= "
This is a heading block.
" + (is (= "
This is a heading block.
" (heading-block/render "##### This is a heading block." nil nil)))) (testing "ATX heading block that's a H6" - (is (= "
This is a heading block.
" + (is (= "
This is a heading block.
" (heading-block/render "###### This is a heading block." nil nil)))) (testing "No H tag when 7 or more # characters" @@ -38,11 +38,11 @@ (heading-block/render "#This is not a heading block." nil nil)))) (testing "ATX heading can precede up to 3 spaces" - (is (= "

This is a heading.

" + (is (= "

This is a heading.

" (heading-block/render " # This is a heading." nil nil))) - (is (= "

This is a heading.

" + (is (= "

This is a heading.

" (heading-block/render " # This is a heading." nil nil))) - (is (= "

This is a heading.

" + (is (= "

This is a heading.

" (heading-block/render " # This is a heading." nil nil)))) (testing "But no more than 3 spaces" @@ -53,17 +53,17 @@ (deftest settext-heading-renderer-text (testing "Settext heading block that's a H1" - (is (= "

This is a heading block.

" + (is (= "

This is a heading block.

" (heading-block/render "This is a heading block.\n=========" nil nil)))) (testing "Settext heading block that's a H1 spanning multiple lines" - (is (= "

This is a \nheading block spanning multiple lines.

" + (is (= "

This is a \nheading block spanning multiple lines.

" (heading-block/render "This is a \nheading block spanning multiple lines.\n========" nil nil)))) (testing "Settext heading block that's a H2" - (is (= "

This is a heading block.

" + (is (= "

This is a heading block.

" (heading-block/render "This is a heading block.\n---------" nil nil)))) (testing "Settext heading block that's a H2 spanning multiple lines" - (is (= "

This is a \nheading block spanning multiple lines.

" + (is (= "

This is a \nheading block spanning multiple lines.

" (heading-block/render "This is a \nheading block spanning multiple lines.\n--------" nil nil))))) -- cgit v1.2.3