fix(player): a description link keeps its sentence's punctuation and its own parentheses
The trailing .,;:!?)] was trimmed off the href and dropped from the text too, and a URL ending in a balanced ")" (a Wikipedia article) was cut short. The trim keeps a closing paren that has its opener, and the trimmed tail is put back after the link.
This commit is contained in:
@@ -53,6 +53,22 @@ function parseYouTube(url: string): { id: string; start: number | null } | null
|
||||
return { id, start: parseStart(u.searchParams.get("t") || u.searchParams.get("start")) };
|
||||
}
|
||||
|
||||
/** The address inside a matched URL: sentence punctuation after it is not part of it, but a ")" that
|
||||
* closes a "(" of its own is (a Wikipedia article's "Mercury_(planet)"). */
|
||||
function trimUrl(raw: string): string {
|
||||
let end = raw.length;
|
||||
for (;;) {
|
||||
const c = raw[end - 1];
|
||||
if (c && ".,;:!?]".includes(c)) end--;
|
||||
else if (c === ")") {
|
||||
const body = raw.slice(0, end);
|
||||
if (body.split(")").length > body.split("(").length) end--;
|
||||
else break;
|
||||
} else break;
|
||||
}
|
||||
return raw.slice(0, end);
|
||||
}
|
||||
|
||||
export function renderDescription(
|
||||
text: string,
|
||||
opts: {
|
||||
@@ -74,7 +90,9 @@ export function renderDescription(
|
||||
.trim();
|
||||
for (const chunk of clean.split(URL_RE)) {
|
||||
if (/^https?:\/\//.test(chunk)) {
|
||||
const href = chunk.replace(/[.,;:!?)\]]+$/, "");
|
||||
const href = trimUrl(chunk);
|
||||
// What was trimmed is the sentence's, not the link's: it stays in the text, after the link.
|
||||
const tail = chunk.slice(href.length);
|
||||
const yt = parseYouTube(href);
|
||||
if (yt) {
|
||||
const sameVideo = yt.id === opts.currentId;
|
||||
@@ -98,6 +116,7 @@ export function renderDescription(
|
||||
</a>,
|
||||
);
|
||||
}
|
||||
if (tail) out.push(<span key={key++}>{tail}</span>);
|
||||
continue;
|
||||
}
|
||||
// Plain text: pull out emails, hashtags and bare timestamps.
|
||||
|
||||
Reference in New Issue
Block a user