|
| 1 | +--- |
| 2 | +import { resolve } from '../i18n/resolve'; |
| 3 | +import { marked } from 'marked'; |
| 4 | +import regulators from '../data/regulators.yaml'; |
| 5 | +
|
| 6 | +interface Props { |
| 7 | + locale: string; |
| 8 | +} |
| 9 | +
|
| 10 | +const { locale } = Astro.props; |
| 11 | +
|
| 12 | +/** Parse inline markdown and add target="_blank" + ↗ to http/https links */ |
| 13 | +function renderInline(md: string): string { |
| 14 | + let html = marked.parseInline(md) as string; |
| 15 | + html = html.replace(/<a href="(https?:\/\/[^"]*)">/g, '<a href="$1" target="_blank">'); |
| 16 | + html = html.replace(/<a href="(https?:\/\/[^"]*)" target="_blank">([^<]*)<\/a>/g, |
| 17 | + '<a href="$1" target="_blank">$2 ↗</a>'); |
| 18 | + return html; |
| 19 | +} |
| 20 | +
|
| 21 | +/** Parse block markdown and add target="_blank" + ↗ to http/https links */ |
| 22 | +function renderBlock(md: string): string { |
| 23 | + let html = marked.parse(md) as string; |
| 24 | + html = html.replace(/<a href="(https?:\/\/[^"]*)">/g, '<a href="$1" target="_blank">'); |
| 25 | + html = html.replace(/<a href="(https?:\/\/[^"]*)" target="_blank">([^<]*)<\/a>/g, |
| 26 | + '<a href="$1" target="_blank">$2 ↗</a>'); |
| 27 | + return html; |
| 28 | +} |
| 29 | +
|
| 30 | +const countries = regulators.countries.filter((c: any) => |
| 31 | + !c.locales || c.locales.includes(locale) |
| 32 | +); |
| 33 | +
|
| 34 | +// Korean locale puts South Korea first |
| 35 | +const orderedCountries = locale === 'ko' |
| 36 | + ? [ |
| 37 | + ...countries.filter((c: any) => c.id === 'south-korea'), |
| 38 | + ...countries.filter((c: any) => c.id !== 'south-korea'), |
| 39 | + ] |
| 40 | + : countries; |
| 41 | +--- |
| 42 | + |
| 43 | +<h3 id="consumers">{resolve(regulators.heading, locale)}</h3> |
| 44 | + |
| 45 | +<Fragment set:html={renderBlock(resolve(regulators.intro, locale))} /> |
| 46 | +<Fragment set:html={renderBlock(resolve(regulators.follow_up, locale))} /> |
| 47 | + |
| 48 | +{orderedCountries.map((country: any) => { |
| 49 | + const items = country.items.filter((item: any) => |
| 50 | + !item.locales || item.locales.includes(locale) |
| 51 | + ); |
| 52 | + return ( |
| 53 | + <> |
| 54 | + <h4>{resolve(country.name, locale)}</h4> |
| 55 | + <ul> |
| 56 | + {items.map((item: any) => ( |
| 57 | + <li set:html={renderInline(resolve(item.text, locale))} /> |
| 58 | + ))} |
| 59 | + </ul> |
| 60 | + </> |
| 61 | + ); |
| 62 | +})} |
0 commit comments