From 071c9af7bbf776f6c4f9a2c41077df4a9212210e Mon Sep 17 00:00:00 2001 From: Nero <41307858+nero@users.noreply.github.com> Date: Sun, 26 Jun 2022 00:24:23 +0000 Subject: [PATCH] Generate id atteributes for headings --- md2html | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/md2html b/md2html index ab5c6d1..11c42b4 100755 --- a/md2html +++ b/md2html @@ -130,25 +130,20 @@ function inline(text) { next } -/^# / { +/^##* / { + d=length($1) gsub(/^#* /, "", $0) - if (!meta["title"]) meta["title"] = $0 + if (d==1 && !meta["title"]) meta["title"] = $0 + id1=$0 + gsub(/[^a-zA-Z0-9]/, "", id1) + hash[d]=id1 + id = "" + for (i = 2; i <= d; i++) { + id = id hash[i] + } + if (id != "") id = " id=\"" escape(id) "\"" level(prefix) - oneliner("h1", $0) - next -} - -/^## / { - gsub(/^#* /, "", $0) - level(prefix) - oneliner("h2", $0) - next -} - -/^### / { - gsub(/^#* /, "", $0) - level(prefix) - oneliner("h3", $0) + oneliner("h"d, $0, id) next }