@prunellejs/i18n 0.7.2 → 0.7.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 +36 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/i18n",
3
- "version": "0.7.2",
3
+ "version": "0.7.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.7.2"
24
+ "prunelle": "^0.7.3"
25
25
  }
26
26
  }
package/src/index.ts CHANGED
@@ -28,12 +28,41 @@ export interface I18nOptions {
28
28
  site?: string | undefined
29
29
  }
30
30
 
31
- const HTML_TOKEN_RE = /<(\/?)(pre|code|textarea)(?=[\s>/])[^>]*>|<a(?=[\s>/])((?:[^>"']|"[^"]*"|'[^']*')*)>/gi
32
- // One attribute of a start tag: its leading space, its name, and the value when it is quoted.
33
- const ATTR_RE = /(\s)([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'))?/g
34
- const NO_LOCALIZE_RE = /\sdata-no-localize(?=[\s=/]|$)/i
31
+ const HTML_TOKEN_RE = /<(\/?)(pre|code|textarea)(?=[\s>/])[^<>]*>|<a(?=[\s>/])((?:[^<>"']|"[^"]*"|'[^']*')*)>/gi
32
+ // One attribute of a start tag: optional leading separator, name, and optional `= value` (quoted or unquoted).
33
+ const ATTR_RE = /([\s/]*)([^\s"'<>/=]+)(?:(\s*=\s*)(?:"([^"]*)"|'([^']*)'|([^\s<>]+)))?/g
35
34
  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
36
35
 
36
+ function rewriteAnchorAttrs(attrs: string, localizeHref: (href: string) => string | undefined): string {
37
+ ATTR_RE.lastIndex = 0
38
+ let seenHref = false
39
+ let hrefReplacement: { start: number, end: number, text: string } | undefined
40
+ let match: RegExpExecArray | null
41
+ while ((match = ATTR_RE.exec(attrs)) !== null) {
42
+ const name = match[2]!.toLowerCase()
43
+ if (name === 'data-no-localize')
44
+ return attrs
45
+ if (name === 'href' && !seenHref) {
46
+ seenHref = true
47
+ const double = match[4]
48
+ const single = match[5]
49
+ const value = double ?? single
50
+ const next = value !== undefined ? localizeHref(value) : undefined
51
+ if (next !== undefined) {
52
+ const quote = double !== undefined ? '"' : '\''
53
+ hrefReplacement = {
54
+ start: match.index,
55
+ end: match.index + match[0].length,
56
+ text: `${match[1]}${match[2]}${match[3]}${quote}${next}${quote}`,
57
+ }
58
+ }
59
+ }
60
+ }
61
+ if (hrefReplacement === undefined)
62
+ return attrs
63
+ return `${attrs.slice(0, hrefReplacement.start)}${hrefReplacement.text}${attrs.slice(hrefReplacement.end)}`
64
+ }
65
+
37
66
  function isSkippedI18nPath(pathname: string, assetPrefix: string, assetPrefixSlash: string): boolean {
38
67
  return pathname === assetPrefix
39
68
  || pathname.startsWith(assetPrefixSlash)
@@ -94,17 +123,10 @@ function localizeHtmlLinks(
94
123
  }
95
124
  return match
96
125
  }
97
- if (inTextarea || codePreDepth > 0 || attrs === undefined || NO_LOCALIZE_RE.test(attrs))
126
+ if (inTextarea || codePreDepth > 0 || attrs === undefined)
98
127
  return match
99
- const rewritten = attrs.replace(ATTR_RE, (attr, space: string, name: string, double: string | undefined, single: string | undefined) => {
100
- const value = double ?? single
101
- const next = name.toLowerCase() === 'href' && value !== undefined ? localizeHref(value) : undefined
102
- if (next === undefined)
103
- return attr
104
- const quote = double !== undefined ? '"' : "'"
105
- return `${space}${name}=${quote}${next}${quote}`
106
- })
107
- return `<a${rewritten}>`
128
+ const rewritten = rewriteAnchorAttrs(attrs, localizeHref)
129
+ return rewritten === attrs ? match : `<a${rewritten}>`
108
130
  },
109
131
  )
110
132
  }