Skip to content

Commit 4d8bf27

Browse files
committed
feat(generator): add HTML entity decoding for code highlighting
- Introduced a method to decode HTML entities in code blocks before applying syntax highlighting, ensuring accurate rendering of special characters. - Updated the documentation generator to utilize this new method, enhancing the quality of generated documentation.
1 parent 272176f commit 4d8bf27

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 📚 Knowledge - Modern Documentation Generator
1+
# 📚 Knowledge
22

33
## 🎯 What is Knowledge?
44

src/generator.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ export class DocumentationGenerator {
142142
}
143143
}
144144

145+
private decodeHtmlEntities(text: string): string {
146+
const entities: Record<string, string> = {
147+
'&quot;': '"',
148+
'&#39;': "'",
149+
'&lt;': '<',
150+
'&gt;': '>',
151+
'&amp;': '&'
152+
};
153+
154+
return text.replace(/&(?:quot|#39|lt|gt|amp);/g, (match) => entities[match] || match);
155+
}
156+
145157
private async processMarkdown(content: string): Promise<string> {
146158
// Processar markdown com syntax highlighting manual
147159
let html = await marked(content);
@@ -150,8 +162,15 @@ export class DocumentationGenerator {
150162
html = html.replace(/<pre><code class="language-(\w+)">([\s\S]*?)<\/code><\/pre>/g, (match, lang, code) => {
151163
if (hljs.getLanguage(lang)) {
152164
try {
153-
const highlighted = hljs.highlight(code, { language: lang }).value;
154-
return `<pre><code class="hljs language-${lang}">${highlighted}</code></pre>`;
165+
// Decodificar entidades HTML antes de aplicar highlight
166+
const decodedCode = this.decodeHtmlEntities(code);
167+
168+
const highlighted = hljs.highlight(decodedCode, { language: lang }).value;
169+
170+
// Decodificar entidades HTML no resultado do highlight.js também
171+
const finalHighlighted = this.decodeHtmlEntities(highlighted);
172+
173+
return `<pre><code class="hljs language-${lang}">${finalHighlighted}</code></pre>`;
155174
} catch (err) {
156175
console.warn(`Failed to highlight code with language "${lang}":`, err);
157176
}

0 commit comments

Comments
 (0)