mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
fix(quality): resolve reliability findings on dev
The Sonar quality gate rated new code D. Preserve focus and keyboard interaction through native controls and shared dialog behavior. Replace ambiguous regex scans with bounded parsing and make numeric coercion and initialization explicit.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { stripMarkdown } from './strip-markdown';
|
||||
import { replaceInlineMarkdownLinks, stripMarkdown, stripPreviewTags } from './strip-markdown';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@@ -51,3 +51,17 @@ describe('stripMarkdown', () => {
|
||||
expect(Date.now() - started).toBeLessThan(1_000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('plain text previews', () => {
|
||||
it('passes the label and target of adjacent links to the formatter', () => {
|
||||
const render = (label: string, target: string) => `${label}: ${target}`;
|
||||
expect(replaceInlineMarkdownLinks('[A](one)[B](two)', render)).toBe('A: oneB: two');
|
||||
expect(replaceInlineMarkdownLinks('[A](one\ntwo)', render)).toBe('[A](one\ntwo)');
|
||||
expect(replaceInlineMarkdownLinks('[A](one\ntwo)', render, true)).toBe('A: one\ntwo');
|
||||
});
|
||||
|
||||
it('separates surrounding words and keeps unfinished tags', () => {
|
||||
expect(stripPreviewTags('one<b>two</b>three<unfinished')).toBe('one two three<unfinished');
|
||||
expect(stripPreviewTags('one<>two', false)).toBe('one<>two');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,7 +11,7 @@ export function stripMarkdown(md: string): string {
|
||||
// instead of being retried against every later "](". Same matches, no backtracking
|
||||
.replace(/!\[(?:(?!\]\().)*\]\(.*?\)/g, ''); // images
|
||||
|
||||
return stripLinks(withoutImages) // links → text
|
||||
return replaceInlineMarkdownLinks(withoutImages, (label) => label)
|
||||
.replace(/(`{3}[\s\S]*?`{3})/g, '') // code blocks
|
||||
.replace(/`([^`]+)`/g, '$1') // inline code
|
||||
.replace(/\*\*(.+?)\*\*/g, '$1') // bold **
|
||||
@@ -53,7 +53,11 @@ const notALineTerminator = /./;
|
||||
* - the target ends at the first `)`, and `.` cannot cross a line terminator, so a `(`
|
||||
* whose first `)` only turns up on a later line is not a link at all.
|
||||
*/
|
||||
function stripLinks(input: string): string {
|
||||
export function replaceInlineMarkdownLinks(
|
||||
input: string,
|
||||
replace: (label: string, target: string, original: string, offset: number) => string,
|
||||
multilineTargets = false,
|
||||
): string {
|
||||
let out = '';
|
||||
let cursor = 0; // everything before this has already been copied into `out`
|
||||
let search = 0; // where the next `[` is looked for
|
||||
@@ -79,16 +83,38 @@ function stripLinks(input: string): string {
|
||||
}
|
||||
|
||||
if (stop < close + 2) stop = close + 2;
|
||||
while (stop < input.length && input[stop] !== ')' && notALineTerminator.test(input.charAt(stop))) stop++;
|
||||
while (
|
||||
stop < input.length &&
|
||||
input[stop] !== ')' &&
|
||||
(multilineTargets || notALineTerminator.test(input.charAt(stop)))
|
||||
)
|
||||
stop++;
|
||||
if (input[stop] !== ')') {
|
||||
search = close + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
out += input.slice(cursor, open) + input.slice(open + 1, close);
|
||||
out +=
|
||||
input.slice(cursor, open) +
|
||||
replace(input.slice(open + 1, close), input.slice(close + 2, stop), input.slice(open, stop + 1), open);
|
||||
cursor = stop + 1;
|
||||
search = stop + 1;
|
||||
}
|
||||
|
||||
return out + input.slice(cursor);
|
||||
}
|
||||
|
||||
/** Separate words on either side of markup when building a plain text preview. */
|
||||
export function stripPreviewTags(input: string, allowEmpty = true): string {
|
||||
const parts: string[] = [];
|
||||
let cursor = 0;
|
||||
for (let open = input.indexOf('<'); open !== -1; open = input.indexOf('<', cursor)) {
|
||||
const close = input.indexOf('>', open + 1);
|
||||
if (close === -1) break;
|
||||
parts.push(input.slice(cursor, allowEmpty || close > open + 1 ? open : close + 1));
|
||||
if (allowEmpty || close > open + 1) parts.push(' ');
|
||||
cursor = close + 1;
|
||||
}
|
||||
parts.push(input.slice(cursor));
|
||||
return parts.join('');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
/** Remove every trailing slash from a URL or path in one pass. */
|
||||
export function stripTrailingSlashes(value: string): string {
|
||||
let end = value.length;
|
||||
while (end > 0 && value[end - 1] === '/') end--;
|
||||
return value.slice(0, end);
|
||||
}
|
||||
Reference in New Issue
Block a user