@terrymooreii/sia 2.3.1 → 2.3.2

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/lib/content.js +62 -1
  2. package/package.json +1 -1
package/lib/content.js CHANGED
@@ -402,10 +402,66 @@ export function getDateFromFilename(filename) {
402
402
  return null;
403
403
  }
404
404
 
405
+ /**
406
+ * Convert relative image and link paths to absolute paths based on base URL
407
+ * @param {string} html - HTML content with potential relative paths
408
+ * @param {string} baseUrl - Base URL for the content item (e.g., '/blog/my-post/')
409
+ * @returns {string} HTML with absolute paths
410
+ */
411
+ function fixRelativePaths(html, baseUrl) {
412
+ if (!html || !baseUrl) return html;
413
+
414
+ // Normalize baseUrl to ensure it ends with a slash
415
+ const normalizedBaseUrl = baseUrl.endsWith('/') ? baseUrl : baseUrl + '/';
416
+
417
+ // Helper function to convert relative path to absolute
418
+ const makeAbsolute = (path) => {
419
+ // Skip if it's already an absolute URL (http://, https://) or absolute path (/)
420
+ if (/^(https?:|\/|#|mailto:)/.test(path)) {
421
+ return path;
422
+ }
423
+
424
+ // Convert relative path to absolute path
425
+ // Remove leading ./ if present
426
+ const cleanPath = path.replace(/^\.\//, '');
427
+
428
+ // Combine base URL with path
429
+ return normalizedBaseUrl + cleanPath;
430
+ };
431
+
432
+ // Fix relative image paths
433
+ html = html.replace(
434
+ /<img\s+([^>]*?)(?:src\s*=\s*(["'])([^"']+)\2)([^>]*)>/gi,
435
+ (match, beforeAttrs, quote, src, afterAttrs) => {
436
+ const absolutePath = makeAbsolute(src);
437
+
438
+ // Reconstruct the img tag with the absolute path
439
+ const before = beforeAttrs ? beforeAttrs.trim() + ' ' : '';
440
+ const after = afterAttrs ? ' ' + afterAttrs.trim() : '';
441
+ return `<img ${before}src=${quote}${absolutePath}${quote}${after}>`;
442
+ }
443
+ );
444
+
445
+ // Fix relative link paths (but skip anchor links and external URLs)
446
+ html = html.replace(
447
+ /<a\s+([^>]*?)(?:href\s*=\s*(["'])([^"']+)\2)([^>]*)>/gi,
448
+ (match, beforeAttrs, quote, href, afterAttrs) => {
449
+ const absolutePath = makeAbsolute(href);
450
+
451
+ // Reconstruct the link tag with the absolute path
452
+ const before = beforeAttrs ? beforeAttrs.trim() + ' ' : '';
453
+ const after = afterAttrs ? ' ' + afterAttrs.trim() : '';
454
+ return `<a ${before}href=${quote}${absolutePath}${quote}${after}>`;
455
+ }
456
+ );
457
+
458
+ return html;
459
+ }
460
+
405
461
  /**
406
462
  * Parse a markdown file with front matter
407
463
  */
408
- export async function parseContent(filePath) {
464
+ export async function parseContent(filePath, options = {}) {
409
465
  let content = readFileSync(filePath, 'utf-8');
410
466
 
411
467
  // Execute beforeParse hook
@@ -546,6 +602,11 @@ export async function loadCollection(config, collectionName) {
546
602
  item.url = basePath + permalink;
547
603
  item.outputPath = join(config.outputDir, permalink, 'index.html');
548
604
 
605
+ // Fix relative image and link paths in content and excerptHtml
606
+ // This ensures images and links work on both individual pages and list pages
607
+ item.content = fixRelativePaths(item.content, item.url);
608
+ item.excerptHtml = fixRelativePaths(item.excerptHtml, item.url);
609
+
549
610
  return item;
550
611
  } catch (err) {
551
612
  console.error(`Error parsing ${filePath}:`, err.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrymooreii/sia",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "description": "A simple, powerful static site generator with markdown, front matter, and Nunjucks templates",
5
5
  "main": "lib/index.js",
6
6
  "bin": {