aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-09-26 22:28:05 +0100
committerMichael Williamson <mike@zwobble.org>2026-09-26 22:28:05 +0100
commitd302d0755e075811a37268f4cef00e7c54cb409a (patch)
tree9ef351b65000d176a1edc39cbeef8bcce5dee6a2 /lib
parent671dbb6ae5738d6bf8f97d13dd08bfc0a23e0b04 (diff)
Escape HTML IDs in markdown writer
Diffstat (limited to 'lib')
-rw-r--r--lib/writers/markdown-writer.js28
1 files changed, 16 insertions, 12 deletions
diff --git a/lib/writers/markdown-writer.js b/lib/writers/markdown-writer.js
index a89ad9f..a5b572e 100644
--- a/lib/writers/markdown-writer.js
+++ b/lib/writers/markdown-writer.js
@@ -1,5 +1,6 @@
var _ = require("underscore");
+var htmlWriter = require("./html-writer");
function symmetricMarkdownElement(end) {
return markdownElement(end, end);
@@ -52,10 +53,10 @@ function markdownListItem(attributes, list, listItem) {
list = list || {indent: 0, isOrdered: false, count: 0};
list.count++;
listItem.hasClosed = false;
-
+
var bullet = list.isOrdered ? list.count + "." : "-";
var start = repeatString("\t", list.indent) + bullet + " ";
-
+
return {
start: start,
end: function() {
@@ -94,20 +95,20 @@ function markdownWriter() {
var elementStack = [];
var list = null;
var listItem = {};
-
+
function open(tagName, attributes) {
attributes = attributes || {};
-
+
var createElement = htmlToMarkdown[tagName] || function() {
return {};
};
var element = createElement(attributes, list, listItem);
elementStack.push({end: element.end, list: list});
-
+
if (element.list) {
list = element.list;
}
-
+
var anchorBeforeStart = element.anchorPosition === "before";
if (anchorBeforeStart) {
writeAnchor(attributes);
@@ -118,29 +119,32 @@ function markdownWriter() {
writeAnchor(attributes);
}
}
-
+
function writeAnchor(attributes) {
if (attributes.id) {
- fragments.push('<a id="' + attributes.id + '"></a>');
+ var writer = htmlWriter.writer();
+ writer.open("a", {id: attributes.id});
+ writer.close("a");
+ fragments.push(writer.asString());
}
}
-
+
function close(tagName) {
var element = elementStack.pop();
list = element.list;
var end = _.isFunction(element.end) ? element.end() : element.end;
fragments.push(end || "");
}
-
+
function selfClosing(tagName, attributes) {
open(tagName, attributes);
close(tagName);
}
-
+
function text(value) {
fragments.push(escapeMarkdown(value));
}
-
+
function asString() {
return fragments.join("");
}