Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function getMarkdownHeaders (lines, start, maxHeaderLevel, minHeaderLevel, synta
, name : extractText(x)
, line : x.loc.start.line
, node : x
, href : undefined // need to find a way to extract from, prev, next & current node
};
}
else {
Expand Down Expand Up @@ -194,6 +195,30 @@ function detectLineEnding(content, defaultEol) {
return eol || defaultEol;
}

function getContentWithAnchors(docNodes, content, config, tocableStart, maxHeaderLevel, minHeaderLevel, eol){
var headers = getMarkdownHeaders(docNodes, tocableStart, maxHeaderLevel, minHeaderLevel);
var newContent = '';
var startIndex = 0;

for (var i = 0; i < headers.length; i++) {
var header = headers[i];

if (header.href === undefined){
newContent += content.slice(startIndex, header.node.range[0]);
newContent += generateHeaderWithAnchor(header.node.raw, config, eol);
startIndex = header.node.range[1] + 1;
}
}

newContent += content.slice(startIndex);
return newContent;
}

function generateHeaderWithAnchor(text, config, eol){
// need to use config to build string
return text;
}

exports = module.exports = function transform(content, mode, maxHeaderLevel, minHeaderLevel, minTocItems, title, notitle, entryPrefix, processAll, updateOnly, syntax, options) {
options ??= {};
options.toc ??= {};
Expand Down Expand Up @@ -280,6 +305,12 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, min

var tocPosition = tocs.positions[0];
var tocableStart = !processAll ? tocPosition.end.range[1] : startPos;

if (options?.anchor?.style && options.anchor.style != 'none'){
content = getContentWithAnchors(docNodes, content, options.anchor, tocableStart, maxHeaderLevel, minHeaderLevel, eol);
docNodes = md.parse(content);
}

var headers = getMarkdownHeaders(docNodes, tocableStart, maxHeaderLevel, minHeaderLevel, syntax)
.concat(getHtmlHeaders(docNodes, tocableStart, maxHeaderLevelHtml, minHeaderLevel, true));

Expand Down