@prunellejs/seo 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 +16 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/seo",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "SEO meta, JSON-LD, sitemap, redirects, and self-hosted fonts plugin for Prunelle",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -21,6 +21,6 @@
21
21
  "directory": "packages/plugin-seo"
22
22
  },
23
23
  "peerDependencies": {
24
- "prunelle": "^0.7.2"
24
+ "prunelle": "^0.7.3"
25
25
  }
26
26
  }
package/src/index.ts CHANGED
@@ -337,6 +337,19 @@ function emitFonts(ctx: BuildPluginContext, fonts: FontEntry[]): void {
337
337
  }
338
338
  }
339
339
 
340
+ const SAFE_SITEMAP_PATH_RE = /^\/[A-Za-z0-9\-._~/]*$/
341
+ const XML_FORBIDDEN_RE = /[\0-\x08\v\f\x0E-\x1F\uFFFE\uFFFF\uD800-\uDFFF]/gu
342
+
343
+ function encodeSitemapPath(urlPath: string): string {
344
+ if (SAFE_SITEMAP_PATH_RE.test(urlPath))
345
+ return urlPath
346
+ const cut = urlPath.search(/[?#]/)
347
+ const rawPath = (cut === -1 ? urlPath : urlPath.slice(0, cut)).replace(XML_FORBIDDEN_RE, '')
348
+ const suffix = cut === -1 ? '' : urlPath.slice(cut)
349
+ const encoded = rawPath.split('/').map(encodeURIComponent).join('/')
350
+ return `${encoded}${suffix}`
351
+ }
352
+
340
353
  function emitSitemap(
341
354
  ctx: FinalizeBuildContext,
342
355
  site: string,
@@ -395,14 +408,14 @@ function emitSitemap(
395
408
  const { basePath } = stripLocaleFromPath(u, i18nCfg)
396
409
  const group = byBase.get(basePath) ?? []
397
410
  const alts = group
398
- .map(a => `<xhtml:link rel="alternate" hreflang="${escXml(a.locale)}" href="${escXml(`${site}${a.path === '/' ? '/' : a.path}`)}"/>`)
411
+ .map(a => `<xhtml:link rel="alternate" hreflang="${escXml(a.locale)}" href="${escXml(`${site}${encodeSitemapPath(a.path)}`)}"/>`)
399
412
  .join('')
400
- return ` <url><loc>${escXml(`${site}${u === '/' ? '/' : u}`)}</loc>${alts}</url>`
413
+ return ` <url><loc>${escXml(`${site}${encodeSitemapPath(u)}`)}</loc>${alts}</url>`
401
414
  })
402
415
  writeSitemaps(ctx, site, entries, ' xmlns:xhtml="http://www.w3.org/1999/xhtml"')
403
416
  }
404
417
  else {
405
- writeSitemaps(ctx, site, sortedUrls.map(u => ` <url><loc>${escXml(`${site}${u === '/' ? '/' : u}`)}</loc></url>`), '')
418
+ writeSitemaps(ctx, site, sortedUrls.map(u => ` <url><loc>${escXml(`${site}${encodeSitemapPath(u)}`)}</loc></url>`), '')
406
419
  }
407
420
  }
408
421