@prunellejs/i18n 0.1.1 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.ts +71 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/i18n",
3
- "version": "0.1.1",
3
+ "version": "0.5.3",
4
4
  "description": "Locale URL rewriting and dictionary internationalization plugin for Prunelle",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -21,6 +21,6 @@
21
21
  "directory": "packages/plugin-i18n"
22
22
  },
23
23
  "peerDependencies": {
24
- "prunelle": "^0.1.1"
24
+ "prunelle": "^0.5.3"
25
25
  }
26
26
  }
package/src/index.ts CHANGED
@@ -25,7 +25,75 @@ export interface I18nOptions {
25
25
  site?: string | undefined
26
26
  }
27
27
 
28
- const INTERNAL_HREF_RE = /<a\b([^>]*?)\bhref="(\/[^"]*)"([^>]*)>/gi
28
+ const HTML_TOKEN_RE = /<(\/?)(pre|code|textarea)(?=[\s>/])[^>]*>|<a\b([^>]*?)\bhref="(\/[^"]*)"([^>]*)>/gi
29
+ const NON_HTML_EXT_RE = /\.(?:xml|json|txt|pdf|ico|png|jpe?g|gif|svg|webp|avif|webmanifest|zip|tar|gz|bz2|xz|7z|css|m?js|cjs|wasm|map|woff2?|ttf|otf|eot|mp3|mp4|webm|ogg|wav|csv|tsv|rss|atom|ics|vcf|epub|docx?|xlsx?|pptx?)$/i
30
+
31
+ function isSkippedI18nPath(pathname: string, assetPrefix: string, assetPrefixSlash: string): boolean {
32
+ return pathname === assetPrefix
33
+ || pathname.startsWith(assetPrefixSlash)
34
+ || pathname === '/_i18n'
35
+ || pathname.startsWith('/_i18n/')
36
+ || pathname === '/api'
37
+ || pathname.startsWith('/api/')
38
+ || pathname === '/auth'
39
+ || pathname.startsWith('/auth/')
40
+ || NON_HTML_EXT_RE.test(pathname)
41
+ }
42
+
43
+ function localizeHtmlLinks(
44
+ html: string,
45
+ locale: string,
46
+ localeSet: ReadonlySet<string>,
47
+ assetPrefix: string,
48
+ ): string {
49
+ const assetPrefixSlash = `${assetPrefix}/`
50
+ const escapedLocale = escAttr(locale)
51
+ let inTextarea = false
52
+ let codePreDepth = 0
53
+ HTML_TOKEN_RE.lastIndex = 0
54
+ return html.replace(
55
+ HTML_TOKEN_RE,
56
+ (match, slash: string | undefined, skipTag: string | undefined, before: string | undefined, href: string | undefined, after: string | undefined) => {
57
+ if (skipTag !== undefined) {
58
+ const tag = skipTag.toLowerCase()
59
+ const isClosing = slash === '/'
60
+ const isSelfClosing = !isClosing && match.endsWith('/>')
61
+ if (inTextarea) {
62
+ if (isClosing && tag === 'textarea')
63
+ inTextarea = false
64
+ }
65
+ else if (tag === 'textarea') {
66
+ if (!isClosing && !isSelfClosing)
67
+ inTextarea = true
68
+ }
69
+ else if (isClosing) {
70
+ if (codePreDepth > 0)
71
+ codePreDepth--
72
+ }
73
+ else if (!isSelfClosing) {
74
+ codePreDepth++
75
+ }
76
+ return match
77
+ }
78
+ if (inTextarea || codePreDepth > 0 || href === undefined)
79
+ return match
80
+ const pre = before ?? ''
81
+ const post = after ?? ''
82
+ if (pre.includes('data-no-localize') || post.includes('data-no-localize') || href.startsWith('//'))
83
+ return match
84
+ const cut = href.search(/[?#]/)
85
+ const pathOnly = cut === -1 ? href : href.slice(0, cut)
86
+ if (isSkippedI18nPath(pathOnly, assetPrefix, assetPrefixSlash))
87
+ return match
88
+ const seg = pathOnly.slice(1).split('/', 1)[0]
89
+ if (seg !== undefined && localeSet.has(seg))
90
+ return match
91
+ // `href` is already extracted from a double-quoted HTML attribute; only escape the prepended locale segment to avoid double-escaping `&amp;`.
92
+ const prefixed = href === '/' ? `/${escapedLocale}` : `/${escapedLocale}${href}`
93
+ return `<a${pre}href="${prefixed}"${post}>`
94
+ },
95
+ )
96
+ }
29
97
 
30
98
  function collapseLeadingSlashes(p: string): string {
31
99
  if (!p)
@@ -166,16 +234,8 @@ export function i18n(options: I18nOptions): PrunellePlugin {
166
234
  if (method !== 'GET' && method !== 'HEAD')
167
235
  return next()
168
236
  const rawPath = ctx.url.pathname
169
- if (
170
- rawPath === activeAssetPrefix
171
- || rawPath.startsWith(assetPrefixSlash)
172
- || rawPath === '/_i18n'
173
- || rawPath.startsWith('/_i18n/')
174
- || rawPath === '/api'
175
- || rawPath.startsWith('/api/')
176
- ) {
237
+ if (isSkippedI18nPath(rawPath, activeAssetPrefix, assetPrefixSlash))
177
238
  return next()
178
- }
179
239
  if (stripLocalePrefix(rawPath, locales).locale !== undefined)
180
240
  return next()
181
241
  const target = localizePath(rawPath, defaultLocale, defaultLocale, locales, true)
@@ -217,20 +277,7 @@ export function i18n(options: I18nOptions): PrunellePlugin {
217
277
  // Prefix internal <a href="/..."> links for non-default locales only
218
278
  if (locale === defaultLocale && !prefixDefault)
219
279
  return html
220
- const assetPrefixSlash = `${activeAssetPrefix}/`
221
- const escapedLocale = escAttr(locale)
222
- return html.replace(INTERNAL_HREF_RE, (match, before: string, href: string, after: string) => {
223
- if (before.includes('data-no-localize') || after.includes('data-no-localize'))
224
- return match
225
- if (href.startsWith('//') || href === activeAssetPrefix || href.startsWith(assetPrefixSlash) || href.startsWith('/_i18n/'))
226
- return match
227
- const seg = href.slice(1).split(/[/?#]/, 1)[0]
228
- if (seg !== undefined && localeSet.has(seg))
229
- return match
230
- // `href` is already extracted from a double-quoted HTML attribute; only escape the prepended locale segment to avoid double-escaping `&amp;`.
231
- const prefixed = href === '/' ? `/${escapedLocale}` : `/${escapedLocale}${href}`
232
- return `<a${before}href="${prefixed}"${after}>`
233
- })
280
+ return localizeHtmlLinks(html, locale, localeSet, activeAssetPrefix)
234
281
  },
235
282
 
236
283
  check(ctx) {