mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-26 07:33:21 +03:00
fix(markdown): escape backslashes in generated table cells
Pipes were escaped but backslashes were not, so a cell ending in `\` turned the added `\|` into `\\|` and the pipe split the cell. The object-array Markdown export also left newlines in cells, which broke the row; they now become `<br>` like in the Markdown table generator. Fixes CodeQL js/incomplete-sanitization alerts in the Markdown table generator and the object-array export. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9b655473bc
commit
3872432a07
4 files changed
+13
-2
No files matched your search
@@ -20,6 +20,10 @@ describe('markdown-table-generator service', () => {
|
|||||||
it('escapes pipes and converts line breaks to br tags', () => {
|
it('escapes pipes and converts line breaks to br tags', () => {
|
||||||
expect(escapeMarkdownTableCell(' foo | bar\nbaz ')).toBe('foo \\| bar<br>baz');
|
expect(escapeMarkdownTableCell(' foo | bar\nbaz ')).toBe('foo \\| bar<br>baz');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('escapes backslashes so a trailing backslash cannot unescape the pipe', () => {
|
||||||
|
expect(escapeMarkdownTableCell('C:\\temp\\|x')).toBe('C:\\\\temp\\\\\\|x');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('generateMarkdownTable', () => {
|
describe('generateMarkdownTable', () => {
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ export function createMarkdownTable({
|
|||||||
|
|
||||||
export function escapeMarkdownTableCell(value: string): string {
|
export function escapeMarkdownTableCell(value: string): string {
|
||||||
return value
|
return value
|
||||||
.replace(/\|/g, '\\|')
|
.replace(/[\\|]/g, '\\$&')
|
||||||
.replace(/\r\n|\r|\n/g, '<br>')
|
.replace(/\r\n|\r|\n/g, '<br>')
|
||||||
.trim();
|
.trim();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ INSERT INTO users (id, name, age) VALUES (2, 'Bob', 30);`);
|
|||||||
expect(result).toContain('| 2 | Bob | 30 |');
|
expect(result).toContain('| 2 | Bob | 30 |');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should escape Markdown table cells', () => {
|
||||||
|
const result = objectArrayToData([{ path: 'C:\\dir\\', note: 'a|`b`\nc' }], 'markdown');
|
||||||
|
expect(result).toContain('| C:\\\\dir\\\\ | a\\|\\`b\\`<br>c |');
|
||||||
|
});
|
||||||
|
|
||||||
it('should export nested JSON format correctly', () => {
|
it('should export nested JSON format correctly', () => {
|
||||||
const result = objectArrayToData(sampleNestedData, 'json', { tableName, nestify: true });
|
const result = objectArrayToData(sampleNestedData, 'json', { tableName, nestify: true });
|
||||||
expect(result).toBe(`[
|
expect(result).toBe(`[
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ function escapeCSV(value: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function escapeMarkdown(value: any) {
|
function escapeMarkdown(value: any) {
|
||||||
return String(value).replace(/\|/g, '\\|').replace(/`/g, '\\`');
|
return String(value)
|
||||||
|
.replace(/[\\|`]/g, '\\$&')
|
||||||
|
.replace(/\r\n|\r|\n/g, '<br>');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function objectArrayToData(
|
export function objectArrayToData(
|
||||||
|
|||||||
Reference in new issue
Block a user