Skip to content

Commit 8430140

Browse files
Rich-HarrisRich Harris
and
Rich Harris
authored
escape html in terminal (sveltejs#267)
Co-authored-by: Rich Harris <[email protected]>
1 parent 8fac2be commit 8430140

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

src/lib/client/adapters/webcontainer/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { WebContainer } from '@webcontainer/api';
22
import base64 from 'base64-js';
33
import AnsiToHtml from 'ansi-to-html';
4-
import { get_depth } from '../../../utils.js';
4+
import { escape_html, get_depth } from '../../../utils.js';
55
import { ready } from '../common/index.js';
66

77
const converter = new AnsiToHtml({
@@ -59,7 +59,7 @@ export async function create(base, error, progress, logs) {
5959
// clear screen
6060
logs.set([]);
6161
} else {
62-
const log = converter.toHtml(chunk);
62+
const log = converter.toHtml(escape_html(chunk));
6363
logs.update(($logs) => [...$logs, log]);
6464
}
6565
}

src/lib/server/markdown.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'prismjs/components/prism-diff.js';
44
import 'prismjs/components/prism-typescript.js';
55
import 'prism-svelte';
66
import { marked } from 'marked';
7+
import { escape_html } from '$lib/utils';
78

89
const languages = {
910
bash: 'bash',
@@ -17,18 +18,6 @@ const languages = {
1718
'': ''
1819
};
1920

20-
/** @type {Record<string, string>} */
21-
const chars = {
22-
'&': '&amp;',
23-
'<': '&lt;',
24-
'>': '&gt;'
25-
};
26-
27-
/** @param {string} html */
28-
function escape(html) {
29-
return html.replace(/[&<>]/g, (c) => chars[c]);
30-
}
31-
3221
const delimiter_substitutes = {
3322
'+++': ' ',
3423
'---': ' ',
@@ -84,7 +73,7 @@ const default_renderer = {
8473

8574
return {
8675
type,
87-
content: escape(content)
76+
content: escape_html(content)
8877
};
8978
});
9079

@@ -99,7 +88,7 @@ const default_renderer = {
9988
const plang = languages[lang];
10089
const highlighted = plang
10190
? PrismJS.highlight(source, PrismJS.languages[plang], language)
102-
: escape(source);
91+
: escape_html(source);
10392

10493
html = `<div class="code-block">${
10594
options.file ? `<span class="filename">${options.file}</span>` : ''

src/lib/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,15 @@
22
export function get_depth(name) {
33
return name.split('/').length - 1;
44
}
5+
6+
/** @type {Record<string, string>} */
7+
const chars = {
8+
'&': '&amp;',
9+
'<': '&lt;',
10+
'>': '&gt;'
11+
};
12+
13+
/** @param {string} html */
14+
export function escape_html(html) {
15+
return html.replace(/[&<>]/g, (c) => chars[c]);
16+
}

0 commit comments

Comments
 (0)