markdown: Fix TypeScript noUncheckedIndexedAccess errors.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-05-30 10:07:59 -07:00
parent 74bfa62d78
commit d8a7d65647
1 changed files with 14 additions and 24 deletions

View File

@ -100,12 +100,12 @@ export function translate_emoticons_to_names({
const prev_char = str[offset - 1]; const prev_char = str[offset - 1];
const next_char = str[offset + match.length]; const next_char = str[offset + match.length];
const symbol_at_start = terminal_symbols.includes(prev_char); const non_space_at_start =
const symbol_at_end = terminal_symbols.includes(next_char); prev_char !== undefined && symbols_except_space.includes(prev_char);
const non_space_at_start = symbols_except_space.includes(prev_char); const non_space_at_end =
const non_space_at_end = symbols_except_space.includes(next_char); next_char !== undefined && symbols_except_space.includes(next_char);
const valid_start = symbol_at_start || offset === 0; const valid_start = prev_char === undefined || terminal_symbols.includes(prev_char);
const valid_end = symbol_at_end || offset === str.length - match.length; const valid_end = next_char === undefined || terminal_symbols.includes(next_char);
if (non_space_at_start && non_space_at_end) { if (non_space_at_start && non_space_at_end) {
// Hello!:)? // Hello!:)?
@ -417,15 +417,11 @@ export function get_topic_links(topic: string): TopicLink[] {
for (const [pattern, {url_template, group_number_to_name}] of get_linkifier_map().entries()) { for (const [pattern, {url_template, group_number_to_name}] of get_linkifier_map().entries()) {
let match; let match;
while ((match = pattern.exec(topic)) !== null) { while ((match = pattern.exec(topic)) !== null) {
const matched_groups = match.slice(1); const template_context = Object.fromEntries(
let i = 0; match
const template_context: Record<string, string> = {}; .slice(1)
while (i < matched_groups.length) { .map((matched_group, i) => [group_number_to_name[i + 1], matched_group]),
const matched_group = matched_groups[i]; );
const current_group = i + 1;
template_context[group_number_to_name[current_group]] = matched_group;
i += 1;
}
const link_url = url_template.expand(template_context); const link_url = url_template.expand(template_context);
// We store the starting index as well, to sort the order of occurrence of the links // We store the starting index as well, to sort the order of occurrence of the links
// in the topic, similar to the logic implemented in zerver/lib/markdown/__init__.py // in the topic, similar to the logic implemented in zerver/lib/markdown/__init__.py
@ -566,15 +562,9 @@ function handleLinkifier({
const item = get_linkifier_map().get(pattern); const item = get_linkifier_map().get(pattern);
assert(item !== undefined); assert(item !== undefined);
const {url_template, group_number_to_name} = item; const {url_template, group_number_to_name} = item;
const template_context = Object.fromEntries(
let current_group = 1; matches.map((match, i) => [group_number_to_name[i + 1], match]),
const template_context: Record<string, LinkifierMatch> = {}; );
for (const match of matches) {
template_context[group_number_to_name[current_group]] = match;
current_group += 1;
}
return url_template.expand(template_context); return url_template.expand(template_context);
} }