@prunellejs/i18n 0.7.4 → 0.8.0

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 (3) hide show
  1. package/llms.txt +1 -1
  2. package/package.json +2 -2
  3. package/src/index.ts +10 -7
package/llms.txt CHANGED
@@ -27,7 +27,7 @@
27
27
  - HTML & build integration:
28
28
  - The link rewrite reads attributes: only `href` (single or double quotes) on an `<a>` is changed, never `data-href`, and a `>` inside another attribute value does not hide it.
29
29
  - Sets `<html lang={locale}>` and `hreflang` links in `head()`.
30
- - Prefixes internal `<a href="/...">` for non-default locales (`data-no-localize` opts a link out, e.g. language switchers; skips `/_pru/`, `/_i18n/`, already-prefixed paths).
30
+ - Prefixes internal `<a href="/...">` and `<form action="/...">` for non-default locales, never inside `<script>`, `<style>`, comments, `<pre>`, `<code>` or `<textarea>` (`data-no-localize` opts a link out, e.g. language switchers; skips `/_pru/`, `/_i18n/`, already-prefixed paths).
31
31
  - Expands `prerenderPaths` across all configured locales and shares `I18N_CONFIG_SERVICE` with `@prunellejs/seo` for multilingual `sitemap.xml`.
32
32
  - Checks & CLI: `prunelle check` fails on translation keys missing vs `defaultLocale`; `prunelle i18n-check` prints missing keys.
33
33
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/i18n",
3
- "version": "0.7.4",
3
+ "version": "0.8.0",
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.4"
24
+ "prunelle": "^0.8.0"
25
25
  }
26
26
  }
package/src/index.ts CHANGED
@@ -28,12 +28,13 @@ export interface I18nOptions {
28
28
  site?: string | undefined
29
29
  }
30
30
 
31
- const HTML_TOKEN_RE = /<(\/?)(pre|code|textarea)(?=[\s>/])[^<>]*>|<a(?=[\s>/])((?:[^<>"']|"[^"]*"|'[^']*')*)>/gi
31
+ // One pass over the page: comments and whole <script>/<style> elements are matched to be skipped, <pre>/<code>/<textarea> tags track depth, <a> and <form> start tags are rewritten.
32
+ const HTML_TOKEN_RE = /<!--[\s\S]*?-->|<(script|style)(?=[\s>/])[^<>]*>[\s\S]*?<\/\1\s*>|<(\/?)(pre|code|textarea)(?=[\s>/])[^<>]*>|<(a|form)(?=[\s>/])((?:[^<>"']|"[^"]*"|'[^']*')*)>/gi
32
33
  // One attribute of a start tag: optional leading separator, name, and optional `= value` (quoted or unquoted).
33
34
  const ATTR_RE = /([\s/]*)([^\s"'<>/=]+)(?:(\s*=\s*)(?:"([^"]*)"|'([^']*)'|([^\s<>]+)))?/g
34
35
  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
35
36
 
36
- function rewriteAnchorAttrs(attrs: string, localizeHref: (href: string) => string | undefined): string {
37
+ function rewriteLinkAttrs(attrs: string, linkAttr: string, localizeHref: (href: string) => string | undefined): string {
37
38
  ATTR_RE.lastIndex = 0
38
39
  let seenHref = false
39
40
  let hrefReplacement: { start: number, end: number, text: string } | undefined
@@ -42,7 +43,7 @@ function rewriteAnchorAttrs(attrs: string, localizeHref: (href: string) => strin
42
43
  const name = match[2]!.toLowerCase()
43
44
  if (name === 'data-no-localize')
44
45
  return attrs
45
- if (name === 'href' && !seenHref) {
46
+ if (name === linkAttr && !seenHref) {
46
47
  seenHref = true
47
48
  const double = match[4]
48
49
  const single = match[5]
@@ -102,7 +103,9 @@ function localizeHtmlLinks(
102
103
  }
103
104
  return html.replace(
104
105
  HTML_TOKEN_RE,
105
- (match, slash: string | undefined, skipTag: string | undefined, attrs: string | undefined) => {
106
+ (match, rawTag: string | undefined, slash: string | undefined, skipTag: string | undefined, linkTag: string | undefined, attrs: string | undefined) => {
107
+ if (rawTag !== undefined || match.startsWith('<!--'))
108
+ return match
106
109
  if (skipTag !== undefined) {
107
110
  const tag = skipTag.toLowerCase()
108
111
  const isClosing = slash === '/'
@@ -124,10 +127,10 @@ function localizeHtmlLinks(
124
127
  }
125
128
  return match
126
129
  }
127
- if (inTextarea || codePreDepth > 0 || attrs === undefined)
130
+ if (inTextarea || codePreDepth > 0 || attrs === undefined || linkTag === undefined)
128
131
  return match
129
- const rewritten = rewriteAnchorAttrs(attrs, localizeHref)
130
- return rewritten === attrs ? match : `<a${rewritten}>`
132
+ const rewritten = rewriteLinkAttrs(attrs, linkTag.toLowerCase() === 'form' ? 'action' : 'href', localizeHref)
133
+ return rewritten === attrs ? match : `<${linkTag}${rewritten}>`
131
134
  },
132
135
  )
133
136
  }