Skip to content

Commit 2d38461

Browse files
committed
Markdown: Adding IDs to headings.
1 parent 4fcd76f commit 2d38461

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/markdown.lua2p

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
--============================================================]]
1616

17-
local markdown = {}
17+
local markdown = {
18+
addIdsToHeadings = true, -- @Incomplete: API. @Doc
19+
}
1820

1921
!(
2022
local WHITESPACE_CHARS = " \t\n\v\f" -- Excluding \r.
@@ -2319,7 +2321,7 @@ function markdown.parse(s)
23192321
parseInline(inlineContainer, contents, linkReferenceDefinitions)
23202322
end
23212323

2322-
-- Fix tight lists and store HTML block contents.
2324+
-- Fix tight lists, add IDs to headings and store HTML block contents.
23232325
local htmlBlocks = {--[[ ["id1"]=html, ... ]]}
23242326
local htmlBlockCount = 0
23252327

@@ -2343,6 +2345,15 @@ function markdown.parse(s)
23432345
end
23442346
end
23452347

2348+
elseif @@CONSTSET{"h1","h2","h3","h4","h5","h6"}[tagName] then
2349+
if markdown.addIdsToHeadings then
2350+
local id = urlize(el:getHtmlText())
2351+
2352+
if not (id == "" or id == "-") then
2353+
el.attr.id = id
2354+
end
2355+
end
2356+
23462357
elseif tagName == "\0" or tagName == "\0\0" then
23472358
htmlBlockCount = htmlBlockCount + 1
23482359
local idStr = F("%d", htmlBlockCount)

0 commit comments

Comments
 (0)