Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 5091e1c

Browse files
fix(utils/code): encode HTML entities
Previously, the encoding was allowing things like `
` to be passed through unencoded. This was leading to some issues in display. The trade off is that we can no longer specify unusual chars as HTML entities in our code blocks, but this seems correct to me.
1 parent f2c3776 commit 5091e1c

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

utils/code.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
var encoder = new require('node-html-encoder').Encoder();
22
module.exports = function(str, inline, lang) {
3-
3+
44
// Encode any HTML entities in the code string
5-
str = encoder.htmlEncode(str);
5+
str = encoder.htmlEncode(str, true);
66

77
// If a language is provided then attach a CSS class to the code element
88
lang = lang ? ' class="lang-' + lang + '"' : '';
9-
9+
1010
str = '<code' + lang + '>' + str + '</code>';
1111

1212
// If not inline then wrap the code element in a pre element

utils/spec/code.spec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ describe("code utility", function() {
77
it("should HTML encode the string", function() {
88
expect(code('<div>&</div>')).toEqual('<pre><code>&lt;div&gt;&amp;&lt;/div&gt;</code></pre>');
99
});
10+
it("should encode HTML entities", function() {
11+
expect(code('<div>&#10;</div>')).toEqual('<pre><code>&lt;div&gt;&amp;#10;&lt;/div&gt;</code></pre>');
12+
});
1013

1114
describe("inline", function() {
1215
it("should only wrap in a code tag", function() {
@@ -19,4 +22,4 @@ describe("code utility", function() {
1922
expect(code('abc', true, 'js')).toEqual('<code class="lang-js">abc</code>');
2023
});
2124
});
22-
});
25+
});

0 commit comments

Comments
 (0)