@prunellejs/seo 0.7.2 → 0.7.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/seo",
3
- "version": "0.7.2",
3
+ "version": "0.7.4",
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.4"
25
25
  }
26
26
  }
package/src/check.ts CHANGED
@@ -4,6 +4,7 @@ import type { CheckPluginContext } from 'prunelle/config'
4
4
  import type { SeoPluginOptions } from './index'
5
5
  import { existsSync, readdirSync, readFileSync } from 'node:fs'
6
6
  import { basename, dirname, join, relative } from 'node:path'
7
+ import { isInside } from 'prunelle/config'
7
8
  import { getAppContext } from 'prunelle/internal/server'
8
9
 
9
10
  const MAX_TITLE = 60
@@ -376,9 +377,22 @@ function checkImageFile(ctx: CheckPluginContext, image: string | undefined): voi
376
377
  const path = image?.trim().split(/[?#]/)[0]
377
378
  if (path === undefined || path === '' || ctx.publicDir === undefined || REMOTE_URL_RE.test(path))
378
379
  return
379
- const file = join(ctx.publicDir, decodeURIComponent(path.replace(/^\/+/, '')))
380
+ const raw = path.replace(/^\/+/, '')
381
+ let decoded = raw
382
+ try {
383
+ decoded = decodeURIComponent(raw)
384
+ }
385
+ catch {
386
+ decoded = raw
387
+ }
388
+ const file = join(ctx.publicDir, decoded)
389
+ const pubRel = relative(ctx.root, ctx.publicDir) || '.'
390
+ if (!isInside(ctx.publicDir, file)) {
391
+ ctx.warn(formatWarn('prunelle.config.ts', undefined, 0, 'missing-image-file', `seo({ image }) is "${image}", which resolves outside ${pubRel}/, so share previews show no image`, `point \`image\` at a file inside ${pubRel}/ or at an absolute URL`))
392
+ return
393
+ }
380
394
  if (!existsSync(file))
381
- ctx.warn(formatWarn('prunelle.config.ts', undefined, 0, 'missing-image-file', `seo({ image }) is "${image}", but ${relative(ctx.root, file)} does not exist, so share previews show no image`, `add the image to ${relative(ctx.root, ctx.publicDir) || '.'}/, or point \`image\` at a file there or at an absolute URL`))
395
+ ctx.warn(formatWarn('prunelle.config.ts', undefined, 0, 'missing-image-file', `seo({ image }) is "${image}", but ${relative(ctx.root, file)} does not exist, so share previews show no image`, `add the image to ${pubRel}/, or point \`image\` at a file there or at an absolute URL`))
382
396
  }
383
397
 
384
398
  function lastOwned(entries: readonly TextEntry[], file: string, code: string, fromLayout: boolean): OwnedEntry | undefined {
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