summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-08-24 14:57:07 +0100
committerPaul Buetow <paul@buetow.org>2024-08-24 14:57:07 +0100
commit818e1db0599119b428436b4c2547b759150dd621 (patch)
tree44317ba89eca917895ca333cc039ffadd881d6dd /lib
parenta0cdf880c32264165f1af87d7f0465d3923ffe88 (diff)
initial nicer markdown ToC support
Diffstat (limited to 'lib')
-rw-r--r--lib/generate.source.sh6
-rw-r--r--lib/html.source.sh10
-rw-r--r--lib/md.source.sh10
3 files changed, 21 insertions, 5 deletions
diff --git a/lib/generate.source.sh b/lib/generate.source.sh
index 8cc3329..aabb600 100644
--- a/lib/generate.source.sh
+++ b/lib/generate.source.sh
@@ -31,6 +31,12 @@ generate::make_link () {
fi
}
+# Markdown internal href format, we use it also for HTML
+generate::internal_link_id () {
+ local -r text="$1"; shift
+ tr '[:upper:]' '[:lower:]' <<< "$text" | tr ' ' '-' | tr -cd 'A-Za-z0-9-'
+}
+
# Add other docs (e.g. images, videos) from Gemtext to output format.
generate::fromgmi_add_docs () {
local -r src="$1"; shift
diff --git a/lib/html.source.sh b/lib/html.source.sh
index f30cb22..382f5e7 100644
--- a/lib/html.source.sh
+++ b/lib/html.source.sh
@@ -27,7 +27,7 @@ html::make_paragraph () {
html::make_heading () {
local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift
local -r level="$1"; shift
- local -r id=$(tr -cd 'A-Za-z0-9' <<< "$text")
+ local -r id="$(generate::internal_link_id "$text")"
if [ "$HTML_VARIANT_TO_USE" = exact ]; then
echo "<h${level} style='display: inline' id='${id}'>$(html::encode "$text")</h${level}><br />"
@@ -161,7 +161,7 @@ html::list::encode () {
done
text="${text/ /}"
- local -r id=$(tr -cd 'A-Za-z0-9' <<< "$text")
+ local -r id="$(generate::internal_link_id "$text")"
echo "<a href='#$id')'>$(html::encode "$text")</a>"
}
@@ -329,15 +329,15 @@ html::test::exact () {
assert::equals "$(html::make_paragraph "$line")" "<span>echo foo 2&gt;&amp;1</span><br />"
line='# Header 1'
- local id=$(tr -cd 'A-Za-z0-9' <<< "$line")
+ local id='header-1'
assert::equals "$(html::make_heading "$line" 1)" "<h1 style='display: inline' id='${id}'>Header 1</h1><br />"
line='## Header 2'
- id=$(tr -cd 'A-Za-z0-9' <<< "$line")
+ id='header-2'
assert::equals "$(html::make_heading "$line" 2)" "<h2 style='display: inline' id='${id}'>Header 2</h2><br />"
line='### Header 3'
- id=$(tr -cd 'A-Za-z0-9' <<< "$line")
+ id='header-3'
assert::equals "$(html::make_heading "$line" 3)" "<h3 style='display: inline' id='${id}'>Header 3</h3><br />"
line='> This is a quote'
diff --git a/lib/md.source.sh b/lib/md.source.sh
index e7bfae1..667d5c9 100644
--- a/lib/md.source.sh
+++ b/lib/md.source.sh
@@ -25,6 +25,12 @@ md::make_link () {
echo "[$descr]($link) "
}
+md::make_toc_link () {
+ local -r descr="$1"; shift
+ local -r text="${descr/ /}"
+ echo "[$descr](#$(generate::internal_link_id "$text"))"
+}
+
# Convert Gemtext to Markdown.
md::fromgmi () {
while IFS='' read -r line; do
@@ -32,6 +38,10 @@ md::fromgmi () {
'=> '*)
generate::make_link md "$line"
;;
+ '* .'*)
+ echo -n '* '
+ md::make_toc_link "${line/\* /}"
+ ;;
*)
echo "$line"
;;