Skip to content

Commit 16873e7

Browse files
[docs-infra] Do not escape pipes (|) inside code blocks (#47139)
1 parent 7204051 commit 16873e7

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

docs/translations/api-docs/button/button.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"description": "The URL to link to when the button is clicked. If defined, an <code>a</code> element will be used as the root node."
2626
},
2727
"loading": {
28-
"description": "If <code>true</code>, the loading indicator is visible and the button is disabled. If <code>true \\| false</code>, the loading wrapper is always rendered before the children to prevent <a href=\"https://github.com/mui/material-ui/issues/27853\">Google Translation Crash</a>."
28+
"description": "If <code>true</code>, the loading indicator is visible and the button is disabled. If <code>true | false</code>, the loading wrapper is always rendered before the children to prevent <a href=\"https://github.com/mui/material-ui/issues/27853\">Google Translation Crash</a>."
2929
},
3030
"loadingIndicator": {
3131
"description": "Element placed before the children if the button is in loading state. The node should contain an element with <code>role=&quot;progressbar&quot;</code> with an accessible name. By default, it renders a <code>CircularProgress</code> that is labeled by the button itself."

docs/translations/api-docs/icon-button/icon-button.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"description": "If given, uses a negative margin to counteract the padding on one side (this is often helpful for aligning the left or right side of the icon with content above or below, without ruining the border size and shape)."
1818
},
1919
"loading": {
20-
"description": "If <code>true</code>, the loading indicator is visible and the button is disabled. If <code>true \\| false</code>, the loading wrapper is always rendered before the children to prevent <a href=\"https://github.com/mui/material-ui/issues/27853\">Google Translation Crash</a>."
20+
"description": "If <code>true</code>, the loading indicator is visible and the button is disabled. If <code>true | false</code>, the loading wrapper is always rendered before the children to prevent <a href=\"https://github.com/mui/material-ui/issues/27853\">Google Translation Crash</a>."
2121
},
2222
"loadingIndicator": {
2323
"description": "Element placed before the children if the button is in loading state. The node should contain an element with <code>role=&quot;progressbar&quot;</code> with an accessible name. By default, it renders a <code>CircularProgress</code> that is labeled by the button itself."
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { expect } from 'chai';
2+
import escapeCell from './escapeCell';
3+
4+
describe('escapeCell', () => {
5+
it('escapes pipes outside backticks', () => {
6+
const input = 'true | false';
7+
const result = escapeCell(input);
8+
expect(result).to.equal('true \\| false');
9+
});
10+
11+
it('does not escape pipes inside single backticks', () => {
12+
const input = '`true | false`';
13+
const result = escapeCell(input);
14+
expect(result).to.equal('`true | false`');
15+
});
16+
17+
it('does not escape pipes inside multiple inline code spans', () => {
18+
const input = 'Use `a | b` and `x | y` here';
19+
const result = escapeCell(input);
20+
expect(result).to.equal('Use `a | b` and `x | y` here');
21+
});
22+
23+
it('escapes pipes in normal text but not inside backticks', () => {
24+
const input = '`a | b` or c | d';
25+
const result = escapeCell(input);
26+
expect(result).to.equal('`a | b` or c \\| d');
27+
});
28+
29+
it('handles strings without any pipes', () => {
30+
const input = 'no pipes here';
31+
const result = escapeCell(input);
32+
expect(result).to.equal('no pipes here');
33+
});
34+
35+
it('keeps < inside code spans but escapes outside', () => {
36+
const input = 'Use <b>bold</b> and `<div>` tags';
37+
const result = escapeCell(input);
38+
expect(result).to.equal('Use &lt;b>bold&lt;/b> and `<div>` tags');
39+
});
40+
41+
it('does not escape pipe at string start or end inside backticks', () => {
42+
const input = '`| start and end |`';
43+
const result = escapeCell(input);
44+
expect(result).to.equal('`| start and end |`');
45+
});
46+
47+
it('escapes pipe at string start or end outside backticks', () => {
48+
const input = '| start | and end |';
49+
const result = escapeCell(input);
50+
expect(result).to.equal('\\| start \\| and end \\|');
51+
});
52+
});
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
function escapePipesOutsideBackticks(value: string): string {
2+
// Split into chunks that are either code spans or normal text.
3+
// Capturing group keeps the code chunks in the array.
4+
return value
5+
.split(/(`[^`]*`)/g)
6+
.map((chunk) => (chunk.startsWith('`') ? chunk : chunk.replace(/\|/g, '\\|')))
7+
.join('');
8+
}
9+
110
export default function escapeCell(value: string): string {
211
// As the pipe is use for the table structure
3-
return value.replace(/</g, '&lt;').replace(/`&lt;/g, '`<').replace(/\|/g, '\\|');
12+
const newValue = escapePipesOutsideBackticks(value);
13+
14+
return newValue.replace(/</g, '&lt;').replace(/`&lt;/g, '`<');
415
}

0 commit comments

Comments
 (0)