Skip to content

Commit 9073469

Browse files
erkamyamanatscott
authored andcommitted
fix(docs-infra): escape the code example header
`buildHeaderElement` interpolated the header into a string that is then parsed as HTML, so markup in a header became an element instead of text. The ten captions on https://angular.dev/guide/i18n/translation-files read `messages.fr.xlf ()`, having turned `(<trans-unit>)` into an empty element. These are the only two headers in the guides containing markup.
1 parent 61b353e commit 9073469

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

  • adev/shared-docs/pipeline/shared/marked/extensions/docs-code/format

adev/shared-docs/pipeline/shared/marked/extensions/docs-code/format/index.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ export function processForApiLinks(fragment: Element, apiEntries: ApiEntries): v
9696
});
9797
}
9898

99+
/** Escapes text that is interpolated into an HTML string. */
100+
function escapeHtml(text: string): string {
101+
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
102+
}
103+
99104
/** Build the header element if a header is provided in the token. */
100105
function buildHeaderElement(token: CodeToken) {
101106
let header = '';
@@ -104,7 +109,7 @@ function buildHeaderElement(token: CodeToken) {
104109
}
105110

106111
if (token.header) {
107-
header += `<h3>${token.header}</h3>`;
112+
header += `<h3>${escapeHtml(token.header)}</h3>`;
108113
}
109114

110115
if (!header) return '';

0 commit comments

Comments
 (0)