@jet-w/astro-blog 0.2.6 → 0.2.7

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": "@jet-w/astro-blog",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "A modern Astro blog theme with Vue and Tailwind CSS support",
5
5
  "type": "module",
6
6
  "exports": {
@@ -325,10 +325,81 @@ const formatDate = (date: Date) => {
325
325
  </header>
326
326
 
327
327
  <!-- 文章内容 -->
328
- <div class="prose prose-slate dark:prose-invert max-w-none prose-lg">
328
+ <div class="prose prose-slate dark:prose-invert max-w-none prose-lg" data-prose-content data-locale-prefix={localePrefix} data-base-url={base}>
329
329
  {Content && <Content />}
330
330
  </div>
331
331
 
332
+ <!-- 修复文章内容中的链接,添加当前语言前缀 -->
333
+ <script is:inline>
334
+ (function() {
335
+ function fixProseLinks() {
336
+ const proseContent = document.querySelector('[data-prose-content]');
337
+ if (!proseContent) return;
338
+
339
+ const localePrefix = proseContent.getAttribute('data-locale-prefix') || '';
340
+ const baseUrl = proseContent.getAttribute('data-base-url') || '';
341
+
342
+ // 如果没有 locale prefix 或 locale prefix 等于 base url,则不需要修复
343
+ // 这意味着当前是默认语言
344
+ if (!localePrefix || localePrefix === baseUrl) return;
345
+
346
+ // 获取 locale 部分 (从 localePrefix 中提取,例如 /base/zh-CN -> zh-CN)
347
+ const localePart = localePrefix.replace(baseUrl, '').replace(/^\//, '');
348
+ if (!localePart) return;
349
+
350
+ // 修复所有内部链接
351
+ proseContent.querySelectorAll('a[href]').forEach(function(link) {
352
+ const href = link.getAttribute('href');
353
+ if (!href) return;
354
+
355
+ // 跳过外部链接、锚点链接、mailto、tel 等
356
+ if (href.startsWith('http://') ||
357
+ href.startsWith('https://') ||
358
+ href.startsWith('//') ||
359
+ href.startsWith('#') ||
360
+ href.startsWith('mailto:') ||
361
+ href.startsWith('tel:') ||
362
+ href.startsWith('data:')) {
363
+ return;
364
+ }
365
+
366
+ // 跳过已经包含 locale 前缀的链接
367
+ if (href.startsWith(localePrefix + '/') || href === localePrefix) {
368
+ return;
369
+ }
370
+
371
+ // 对于以 baseUrl 开头但没有 locale 的链接,添加 locale
372
+ if (baseUrl && href.startsWith(baseUrl + '/')) {
373
+ const pathAfterBase = href.slice(baseUrl.length);
374
+ // 检查是否已经有 locale 前缀
375
+ if (!pathAfterBase.startsWith('/' + localePart + '/') && pathAfterBase !== '/' + localePart) {
376
+ link.setAttribute('href', baseUrl + '/' + localePart + pathAfterBase);
377
+ }
378
+ return;
379
+ }
380
+
381
+ // 对于绝对路径链接 (以 / 开头),添加 locale prefix
382
+ if (href.startsWith('/')) {
383
+ // 检查是否已经有 locale 前缀
384
+ if (!href.startsWith('/' + localePart + '/') && href !== '/' + localePart) {
385
+ link.setAttribute('href', localePrefix + href);
386
+ }
387
+ }
388
+ });
389
+ }
390
+
391
+ // 页面加载后执行
392
+ if (document.readyState === 'loading') {
393
+ document.addEventListener('DOMContentLoaded', fixProseLinks);
394
+ } else {
395
+ fixProseLinks();
396
+ }
397
+
398
+ // 支持 Astro 视图过渡
399
+ document.addEventListener('astro:page-load', fixProseLinks);
400
+ })();
401
+ </script>
402
+
332
403
  <!-- 文章底部 -->
333
404
  <footer class="not-prose mt-16 pt-8 border-t border-slate-200 dark:border-slate-700">
334
405
  <!-- 分类信息 -->