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:
@@ -52,11 +52,12 @@ export function composeCover(
|
||||
c: ComposeCtx,
|
||||
words: CoverWords,
|
||||
photo: AutoPhoto | null,
|
||||
more: CoverMaterial = { photos: photo ? [photo] : [], entries: [] },
|
||||
more?: CoverMaterial,
|
||||
): BookSpread {
|
||||
const archetype = c.directives.cover?.archetype;
|
||||
if (archetype) return composeArchetypeCover(c, archetype, words, more);
|
||||
if (c.directives.cover?.lockup) return lockupCover(c, words, more);
|
||||
const material = more ?? { photos: photo ? [photo] : [], entries: [] };
|
||||
if (archetype) return composeArchetypeCover(c, archetype, words, material);
|
||||
if (c.directives.cover?.lockup) return lockupCover(c, words, material);
|
||||
const { pageWidth: PW, pageHeight: H, bleed: b, margin: m } = c.grid;
|
||||
const els: BookElement[] = [];
|
||||
const title = titleWords(words.ownTitle, words.title);
|
||||
|
||||
@@ -38,7 +38,7 @@ function hash(seed: number, key: string): number {
|
||||
export function designRng(seed: number, key: string): DesignRng {
|
||||
let state = hash(seed, key);
|
||||
const next = () => {
|
||||
state = (state + 0x6d2b79f5) | 0;
|
||||
state = Math.imul(state + 0x6d2b79f5, 1);
|
||||
let t = Math.imul(state ^ (state >>> 15), 1 | state);
|
||||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -12,6 +12,7 @@ export * from './common/primitives.schema';
|
||||
export * from './common/pagination.schema';
|
||||
export * from './common/compare';
|
||||
export * from './common/strip-markdown';
|
||||
export * from './common/strip-trailing-slashes';
|
||||
|
||||
// Domain contracts
|
||||
export * from './weather/weather.schema';
|
||||
|
||||
@@ -16,7 +16,7 @@ const MS_PER_DAY = 86_400_000;
|
||||
* wherever the clock sits east of Greenwich.
|
||||
*/
|
||||
export function addIsoDays(date: string, n: number): string {
|
||||
const [y = NaN, m = NaN, d = NaN] = date.split('-').map(Number);
|
||||
const [y = Number.NaN, m = Number.NaN, d = Number.NaN] = date.split('-').map(Number);
|
||||
const next = new Date(Date.UTC(y, m - 1, d) + n * MS_PER_DAY);
|
||||
const yyyy = next.getUTCFullYear();
|
||||
const mm = String(next.getUTCMonth() + 1).padStart(2, '0');
|
||||
|
||||
@@ -26,7 +26,7 @@ export const MAX_TRIP_DAYS = 999;
|
||||
/** Calendar days in an inclusive YYYY-MM-DD range, counted the same way on both sides of the wire. */
|
||||
export function tripSpanDays(startDate: string, endDate: string): number {
|
||||
const utcDay = (date: string) => {
|
||||
const [y = NaN, m = NaN, d = NaN] = date.split('-').map(Number);
|
||||
const [y = Number.NaN, m = Number.NaN, d = Number.NaN] = date.split('-').map(Number);
|
||||
return Date.UTC(y, m - 1, d);
|
||||
};
|
||||
return Math.floor((utcDay(endDate) - utcDay(startDate)) / 86400000) + 1;
|
||||
|
||||
Reference in New Issue
Block a user