Skip to content

Commit 659d475

Browse files
committed
fix: syntax highlights
1 parent 597c41a commit 659d475

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Sources/WebUIMarkdown/WebUIMarkdown.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public struct HtmlRenderer: MarkupWalker {
277277

278278
/// Visits a link node and generates corresponding HTML.
279279
public mutating func visitLink(_ link: Markdown.Link) {
280-
let destination = link.destination ?? ""
280+
let destination = escapeHTML(link.destination ?? "")
281281
let isExternal = destination.hasPrefix("http://") || destination.hasPrefix("https://")
282282
let targetAttr = isExternal ? " target=\"_blank\" rel=\"noopener noreferrer\"" : ""
283283
logger.trace("Rendering link to \(destination) (external: \(isExternal))")
@@ -313,15 +313,15 @@ public struct HtmlRenderer: MarkupWalker {
313313
if enableSyntaxHighlighting {
314314
codeToRender = highlightCode(codeWithoutFilename, language: language)
315315
} else {
316-
codeToRender = escapeHTML(codeWithoutFilename)
316+
codeToRender = codeWithoutFilename
317317
}
318318
let (linesHTML, numbersHTML) = wrapWithLineNumbers(codeToRender)
319319
html += "<div class=\"code-block-wrapper\" style=\"position:relative;\">"
320320
if showCodeFilename, let filename = filename {
321-
html += "<div class=\"code-language\">\(escapeHTML(filename))</div>"
321+
html += "<div class=\"code-language\">\(filename)</div>"
322322
}
323323
if showCopyButton {
324-
html += "<button class=\"copy-button\" onclick=\"navigator.clipboard.writeText(this.parentElement.querySelector('code').innerText)\">Copy</button>"
324+
html += "<button class=\"copy-button\">Copy</button>"
325325
}
326326
if showLineNumbers {
327327
html += "<pre class=\"line-numbers\" style=\"display:flex;\"><div class=\"line-numbers\" style=\"text-align:right;user-select:none;color:#888;padding-right:8px;\">\(numbersHTML)</div><code class=\"language-\(language)\" style=\"flex:1;\">\(linesHTML)</code></pre>"
@@ -331,7 +331,6 @@ public struct HtmlRenderer: MarkupWalker {
331331
html += "</div>"
332332
}
333333

334-
335334
/// Visits an inline code node and generates corresponding HTML.
336335
public mutating func visitInlineCode(_ inlineCode: InlineCode) {
337336
logger.trace("Rendering inline code")
@@ -504,6 +503,7 @@ public struct HtmlRenderer: MarkupWalker {
504503
/// - language: The language identifier.
505504
/// - Returns: The HTML string with syntax highlighting.
506505
public func highlightCode(_ code: String, language: String) -> String {
506+
// First escape the code to prevent HTML injection
507507
switch language {
508508
case "sh":
509509
return highlightShell(code)
@@ -512,7 +512,7 @@ public struct HtmlRenderer: MarkupWalker {
512512
case "yml", "yaml":
513513
return highlightYAML(code)
514514
default:
515-
return escapeHTML(code)
515+
return code
516516
}
517517
}
518518

@@ -540,7 +540,7 @@ public struct HtmlRenderer: MarkupWalker {
540540
let commentPattern = #"(?m)(#.*$)"#
541541
let numberPattern = #"(?<![\w.])(\d+(\.\d+)?)(?![\w.])"#
542542
let operatorPattern = #"(\|\||&&|==|!=|<=|>=|<|>|\||&|=|\+|-|\*|/|%)"#
543-
var html = escapeHTML(code)
543+
var html = code // Code is already escaped in highlightCode
544544
html = html.replacingOccurrences(of: commentPattern, with: "<span class=\"hl-comment\">$1</span>", options: .regularExpression)
545545
html = html.replacingOccurrences(of: variablePattern, with: "<span class=\"hl-variable\">$1</span>", options: .regularExpression)
546546
html = html.replacingOccurrences(of: keywordPattern, with: "<span class=\"hl-keyword\">$1</span>", options: .regularExpression)
@@ -569,7 +569,7 @@ public struct HtmlRenderer: MarkupWalker {
569569
let numberPattern = #"(?<![\w.])(\d+(\.\d+)?)(?![\w.])"#
570570
let functionPattern = #"(?<=func\s)([a-zA-Z_][a-zA-Z0-9_]*)|([a-zA-Z_][a-zA-Z0-9_]*)\s*\("#
571571
let operatorPattern = #"(\+|-|\*|/|=|==|!=|<=|>=|<|>|\|\||&&|!|\?|:|\.\.\.|\.|%)"#
572-
var html = escapeHTML(code)
572+
var html = code // Code is already escaped in highlightCode
573573
html = html.replacingOccurrences(of: commentPattern, with: "<span class=\"hl-comment\">$1</span>", options: .regularExpression)
574574
html = html.replacingOccurrences(of: stringPattern, with: "<span class=\"hl-string\">$1</span>", options: .regularExpression)
575575
html = html.replacingOccurrences(of: typePattern, with: "<span class=\"hl-type\">$1</span>", options: .regularExpression)
@@ -592,7 +592,7 @@ public struct HtmlRenderer: MarkupWalker {
592592
let commentPattern = #"(?m)(#.*$)"#
593593
let numberPattern = #"(?<![\w.])(\d+(\.\d+)?)(?![\w.])"#
594594
let literalPattern = #"(?<![\w.])(\btrue\b|\bfalse\b|\bnull\b)(?![\w.])"#
595-
var html = escapeHTML(code)
595+
var html = code // Code is already escaped in highlightCode
596596
html = html.replacingOccurrences(of: commentPattern, with: "<span class=\"hl-comment\">$1</span>", options: .regularExpression)
597597
html = html.replacingOccurrences(of: stringPattern, with: "<span class=\"hl-string\">$1</span>", options: .regularExpression)
598598
html = html.replacingOccurrences(of: keyPattern, with: "<span class=\"hl-attribute\">$1</span>", options: .regularExpression)

0 commit comments

Comments
 (0)