metalsmith-markdown-partials 2.5.1 → 2.5.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/README.md CHANGED
@@ -12,6 +12,7 @@ A Metalsmith plugin that enables the use of Markdown partials.
12
12
  Markdown fragments are inserted into the contents of a page markdown file by replacing an include marker with markdown partials. This allows for modular markdown and promotes reuse of content.
13
13
 
14
14
  ## Features
15
+
15
16
  - This plugin supports both ESM and CommonJS environments with no configuration needed:
16
17
  - ESM: `import mdPartials from 'metalsmith-markdown-partials'`
17
18
  - CommonJS: `const mdPartials = require('metalsmith-markdown-partials')`
package/lib/index.cjs CHANGED
@@ -27,7 +27,10 @@ const defaults = {
27
27
  * @returns {Options} Normalized options
28
28
  */
29
29
  function normalizeOptions(options) {
30
- return Object.assign({}, defaults, options || {});
30
+ return {
31
+ ...defaults,
32
+ ...(options || {})
33
+ };
31
34
  }
32
35
 
33
36
  /**
@@ -175,10 +178,5 @@ function initMarkdownPartials(options) {
175
178
  };
176
179
  }
177
180
 
178
- // CommonJS export compatibility
179
- if (typeof module !== 'undefined') {
180
- module.exports = initMarkdownPartials;
181
- }
182
-
183
181
  module.exports = initMarkdownPartials;
184
182
  //# sourceMappingURL=index.cjs.map
package/lib/index.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/index.js"],"sourcesContent":["/**\n * A Metalsmith plugin to merge markdown partials into main markdown files.\n *\n * @module metalsmith-markdown-partials\n */\n\n/**\n * @typedef {Object} Options\n * @property {String} libraryPath - Path to the markdown partials library (defaults to './src/content/md-library/')\n * @property {String} fileSuffix - File suffix for markdown files (defaults to '.md')\n */\n\n// Define debug namespace at the top of the file\nconst debugNs = 'metalsmith-markdown-partials';\n\n/** @type {Options} */\nconst defaults = {\n libraryPath: './src/content/md-library/',\n fileSuffix: '.md'\n};\n\n/**\n * Normalize plugin options by merging with defaults\n * @param {Options} [options] - User provided options\n * @returns {Options} Normalized options\n */\nfunction normalizeOptions(options) {\n return Object.assign({}, defaults, options || {});\n}\n\n/**\n * Extract markdown partials from files and prepare them for insertion\n *\n * @param {Object} files - The metalsmith file object\n * @param {Options} options - Plugin options\n * @param {Function} debug - Debug function\n * @returns {Array} Array with all markdown include objects\n */\nfunction getMarkdownIncludes(files, options, debug) {\n const markdownIncludes = [];\n\n // Extract the library name from the path\n const libraryPath = options.libraryPath.slice(0, -1).split('/');\n const libraryName = libraryPath[libraryPath.length - 1];\n\n debug('Processing markdown files with libraryName: %s', libraryName);\n\n // Regex for matching include markers\n const markerRegex = /\\{#md\\s*\".+?\"\\s*#\\}/g;\n const markerStart = '{#md';\n const markerEnd = '#}';\n\n // Set to track already processed partials to prevent duplicates\n const processedPartials = new Set();\n\n Object.keys(files).forEach((file) => {\n /*\n * checks if string 'file' ends with options.fileSuffix\n * when metalsmith-in-place or metalsmith-layouts are used\n * the suffix depends on what templating language is used\n * for example with Nunjucks it would be .md.njk\n *\n * Also check that file does NOT start with libraryName as\n * the markdown partials library is also located in the content folder\n */\n if (file.endsWith(options.fileSuffix) && !file.startsWith(libraryName)) {\n const str = files[file].contents.toString();\n\n // Check if markers are present\n const matches = str.match(markerRegex);\n if (!matches) {return;}\n\n debug('Found %d markdown partials in %s', matches.length, file);\n\n // Process each marker in the file\n matches.forEach((marker) => {\n // Extract the filename from the marker\n const markerFileName = marker.replaceAll(' ', '').replace(`${markerStart}\"`, '').replace(`\"${markerEnd}`, '');\n const partialKey = `${libraryName}/${markerFileName}`;\n\n // Check if partial file exists\n if (!files[partialKey]) {\n debug('Warning: Partial file not found: %s', partialKey);\n return;\n }\n\n // Skip if we've already processed this exact marker+file combination\n const combinedKey = `${file}:${marker}`;\n if (processedPartials.has(combinedKey)) {return;}\n\n processedPartials.add(combinedKey);\n\n // Get the replacement content\n const replacementString = files[partialKey].contents.toString();\n\n markdownIncludes.push({\n marker,\n markerReplacement: replacementString,\n file\n });\n });\n }\n });\n\n // Remove markdown-partials from metalsmith build process\n Object.keys(files).forEach((file) => {\n if (file.startsWith(libraryName)) {\n delete files[file];\n }\n });\n\n debug('Processed %d markdown includes', markdownIncludes.length);\n return markdownIncludes;\n}\n\n/**\n * Replace markers with their markdown replacement strings\n *\n * @param {Object} files - The metalsmith file object\n * @param {Array} markdownIncludes - Array with all markdown include objects\n * @param {Function} debug - Debug function\n * @return {void}\n */\nfunction resolveMarkdownIncludes(files, markdownIncludes, debug) {\n // replace all markers with their markdown replacements\n markdownIncludes.forEach((markdownInclude) => {\n const fileData = files[markdownInclude.file];\n\n // replace the include marker with the actual include file content\n try {\n const contents = fileData.contents.toString();\n fileData.contents = Buffer.from(contents.replace(markdownInclude.marker, markdownInclude.markerReplacement));\n debug('Replaced marker in %s', markdownInclude.file);\n } catch (e) {\n debug('Error replacing marker in %s: %s', markdownInclude.file, e.message);\n console.error(`Error replacing marker in ${markdownInclude.file}:`, e);\n }\n });\n}\n\n/**\n * A Metalsmith plugin to merge markdown partials into main markdown file\n *\n * A marker of the form {#md \"<file name>.md\" #} indicates where the partial\n * must be inserted and also provides the file name of the replacement markdown.\n *\n * @param {Options} [options] - Plugin options\n * @returns {import('metalsmith').Plugin} Metalsmith plugin function\n * @example\n * // In your metalsmith build:\n * .use(markdownPartials({\n * libraryPath: './src/content/md-partials/',\n * fileSuffix: '.md.njk'\n * }))\n */\nfunction initMarkdownPartials(options) {\n options = normalizeOptions(options);\n\n return function markdownPartials(files, metalsmith, done) {\n // Use metalsmith's debug method if available\n const debug = metalsmith.debug ? metalsmith.debug(debugNs) : () => {};\n debug('Running with options: %o', options);\n\n try {\n // Get all markdown includes\n const markdownIncludes = getMarkdownIncludes(files, options, debug);\n\n // Replace include markers with their respective replacement\n if (markdownIncludes.length) {\n resolveMarkdownIncludes(files, markdownIncludes, debug);\n }\n\n setImmediate(done);\n } catch (err) {\n debug('Error processing markdown partials: %s', err.message);\n setImmediate(() => done(err));\n }\n };\n}\n\n// ESM export\nexport default initMarkdownPartials;\n\n// CommonJS export compatibility\nif (typeof module !== 'undefined') {\n module.exports = initMarkdownPartials;\n}\n"],"names":["debugNs","defaults","libraryPath","fileSuffix","normalizeOptions","options","Object","assign","getMarkdownIncludes","files","debug","markdownIncludes","slice","split","libraryName","length","markerRegex","markerStart","markerEnd","processedPartials","Set","keys","forEach","file","endsWith","startsWith","str","contents","toString","matches","match","marker","markerFileName","replaceAll","replace","partialKey","combinedKey","has","add","replacementString","push","markerReplacement","resolveMarkdownIncludes","markdownInclude","fileData","Buffer","from","e","message","console","error","initMarkdownPartials","markdownPartials","metalsmith","done","setImmediate","err","module","exports"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA,MAAMA,OAAO,GAAG,8BAA8B,CAAA;;AAE9C;AACA,MAAMC,QAAQ,GAAG;AACfC,EAAAA,WAAW,EAAE,2BAA2B;AACxCC,EAAAA,UAAU,EAAE,KAAA;AACd,CAAC,CAAA;;AAED;AACA;AACA;AACA;AACA;AACA,SAASC,gBAAgBA,CAACC,OAAO,EAAE;AACjC,EAAA,OAAOC,MAAM,CAACC,MAAM,CAAC,EAAE,EAAEN,QAAQ,EAAEI,OAAO,IAAI,EAAE,CAAC,CAAA;AACnD,CAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASG,mBAAmBA,CAACC,KAAK,EAAEJ,OAAO,EAAEK,KAAK,EAAE;EAClD,MAAMC,gBAAgB,GAAG,EAAE,CAAA;;AAE3B;AACA,EAAA,MAAMT,WAAW,GAAGG,OAAO,CAACH,WAAW,CAACU,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAACC,KAAK,CAAC,GAAG,CAAC,CAAA;EAC/D,MAAMC,WAAW,GAAGZ,WAAW,CAACA,WAAW,CAACa,MAAM,GAAG,CAAC,CAAC,CAAA;AAEvDL,EAAAA,KAAK,CAAC,gDAAgD,EAAEI,WAAW,CAAC,CAAA;;AAEpE;EACA,MAAME,WAAW,GAAG,sBA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EAAED,KAAK,CAAC,CAAA;AACzD,OAAA;MAEA6C,YAAY,CAACD,IAAI,CAAC,CAAA;KACnB,CAAC,OAAOE,GAAG,EAAE;AACZ9C,MAAAA,KAAK,CAAC,wCAAwC,EAAE8C,GAAG,CAACR,OAAO,CAAC,CAAA;AAC5DO,MAAAA,YAAY,CAAC,MAAMD,IAAI,CAACE,GAAG,CAAC,CAAC,CAAA;AAC/B,KAAA;GACD,CAAA;AACH,CAAA;;AAKA;AACA,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE;EACjCA,MAAM,CAACC,OAAO,GAAGP,oBAAoB,CAAA;AACvC;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/index.js"],"sourcesContent":["/**\n * A Metalsmith plugin to merge markdown partials into main markdown files.\n *\n * @module metalsmith-markdown-partials\n */\n\n/**\n * @typedef {Object} Options\n * @property {String} libraryPath - Path to the markdown partials library (defaults to './src/content/md-library/')\n * @property {String} fileSuffix - File suffix for markdown files (defaults to '.md')\n */\n\n// Define debug namespace at the top of the file\nconst debugNs = 'metalsmith-markdown-partials';\n\n/** @type {Options} */\nconst defaults = {\n libraryPath: './src/content/md-library/',\n fileSuffix: '.md'\n};\n\n/**\n * Normalize plugin options by merging with defaults\n * @param {Options} [options] - User provided options\n * @returns {Options} Normalized options\n */\nfunction normalizeOptions(options) {\n return { ...defaults, ...(options || {}) };\n}\n\n/**\n * Extract markdown partials from files and prepare them for insertion\n *\n * @param {Object} files - The metalsmith file object\n * @param {Options} options - Plugin options\n * @param {Function} debug - Debug function\n * @returns {Array} Array with all markdown include objects\n */\nfunction getMarkdownIncludes(files, options, debug) {\n const markdownIncludes = [];\n\n // Extract the library name from the path\n const libraryPath = options.libraryPath.slice(0, -1).split('/');\n const libraryName = libraryPath[libraryPath.length - 1];\n\n debug('Processing markdown files with libraryName: %s', libraryName);\n\n // Regex for matching include markers\n const markerRegex = /\\{#md\\s*\".+?\"\\s*#\\}/g;\n const markerStart = '{#md';\n const markerEnd = '#}';\n\n // Set to track already processed partials to prevent duplicates\n const processedPartials = new Set();\n\n Object.keys(files).forEach((file) => {\n /*\n * checks if string 'file' ends with options.fileSuffix\n * when metalsmith-in-place or metalsmith-layouts are used\n * the suffix depends on what templating language is used\n * for example with Nunjucks it would be .md.njk\n *\n * Also check that file does NOT start with libraryName as\n * the markdown partials library is also located in the content folder\n */\n if (file.endsWith(options.fileSuffix) && !file.startsWith(libraryName)) {\n const str = files[file].contents.toString();\n\n // Check if markers are present\n const matches = str.match(markerRegex);\n if (!matches) {\n return;\n }\n\n debug('Found %d markdown partials in %s', matches.length, file);\n\n // Process each marker in the file\n matches.forEach((marker) => {\n // Extract the filename from the marker\n const markerFileName = marker.replaceAll(' ', '').replace(`${markerStart}\"`, '').replace(`\"${markerEnd}`, '');\n const partialKey = `${libraryName}/${markerFileName}`;\n\n // Check if partial file exists\n if (!files[partialKey]) {\n debug('Warning: Partial file not found: %s', partialKey);\n return;\n }\n\n // Skip if we've already processed this exact marker+file combination\n const combinedKey = `${file}:${marker}`;\n if (processedPartials.has(combinedKey)) {\n return;\n }\n\n processedPartials.add(combinedKey);\n\n // Get the replacement content\n const replacementString = files[partialKey].contents.toString();\n\n markdownIncludes.push({\n marker,\n markerReplacement: replacementString,\n file\n });\n });\n }\n });\n\n // Remove markdown-partials from metalsmith build process\n Object.keys(files).forEach((file) => {\n if (file.startsWith(libraryName)) {\n delete files[file];\n }\n });\n\n debug('Processed %d markdown includes', markdownIncludes.length);\n return markdownIncludes;\n}\n\n/**\n * Replace markers with their markdown replacement strings\n *\n * @param {Object} files - The metalsmith file object\n * @param {Array} markdownIncludes - Array with all markdown include objects\n * @param {Function} debug - Debug function\n * @return {void}\n */\nfunction resolveMarkdownIncludes(files, markdownIncludes, debug) {\n // replace all markers with their markdown replacements\n markdownIncludes.forEach((markdownInclude) => {\n const fileData = files[markdownInclude.file];\n\n // replace the include marker with the actual include file content\n try {\n const contents = fileData.contents.toString();\n fileData.contents = Buffer.from(contents.replace(markdownInclude.marker, markdownInclude.markerReplacement));\n debug('Replaced marker in %s', markdownInclude.file);\n } catch (e) {\n debug('Error replacing marker in %s: %s', markdownInclude.file, e.message);\n console.error(`Error replacing marker in ${markdownInclude.file}:`, e);\n }\n });\n}\n\n/**\n * A Metalsmith plugin to merge markdown partials into main markdown file\n *\n * A marker of the form {#md \"<file name>.md\" #} indicates where the partial\n * must be inserted and also provides the file name of the replacement markdown.\n *\n * @param {Options} [options] - Plugin options\n * @returns {import('metalsmith').Plugin} Metalsmith plugin function\n * @example\n * // In your metalsmith build:\n * .use(markdownPartials({\n * libraryPath: './src/content/md-partials/',\n * fileSuffix: '.md.njk'\n * }))\n */\nfunction initMarkdownPartials(options) {\n options = normalizeOptions(options);\n\n return function markdownPartials(files, metalsmith, done) {\n // Use metalsmith's debug method if available\n const debug = metalsmith.debug ? metalsmith.debug(debugNs) : () => {};\n debug('Running with options: %o', options);\n\n try {\n // Get all markdown includes\n const markdownIncludes = getMarkdownIncludes(files, options, debug);\n\n // Replace include markers with their respective replacement\n if (markdownIncludes.length) {\n resolveMarkdownIncludes(files, markdownIncludes, debug);\n }\n\n setImmediate(done);\n } catch (err) {\n debug('Error processing markdown partials: %s', err.message);\n setImmediate(() => done(err));\n }\n };\n}\n\n// ESM export\nexport default initMarkdownPartials;\n"],"names":["debugNs","defaults","libraryPath","fileSuffix","normalizeOptions","options","getMarkdownIncludes","files","debug","markdownIncludes","slice","split","libraryName","length","markerRegex","markerStart","markerEnd","processedPartials","Set","Object","keys","forEach","file","endsWith","startsWith","str","contents","toString","matches","match","marker","markerFileName","replaceAll","replace","partialKey","combinedKey","has","add","replacementString","push","markerReplacement","resolveMarkdownIncludes","markdownInclude","fileData","Buffer","from","e","message","console","error","initMarkdownPartials","markdownPartials","metalsmith","done","setImmediate","err"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA,MAAMA,OAAO,GAAG,8BAA8B,CAAA;;AAE9C;AACA,MAA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CAAC,CAAC,CAAA;AACxE,KAAA;AACF,GAAC,CAAC,CAAA;AACJ,CAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASI,oBAAoBA,CAAC7C,OAAO,EAAE;AACrCA,EAAAA,OAAO,GAAGD,gBAAgB,CAACC,OAAO,CAAC,CAAA;EAEnC,OAAO,SAAS8C,gBAAgBA,CAAC5C,KAAK,EAAE6C,UAAU,EAAEC,IAAI,EAAE;AACxD;AACA,IAAA,MAAM7C,KAAK,GAAG4C,UAAU,CAAC5C,KAAK,GAAG4C,UAAU,CAAC5C,KAAK,CAACR,OAAO,CAAC,GAAG,MAAM,EAAE,CAAA;AACrEQ,IAAAA,KAAK,CAAC,0BAA0B,EAAEH,OAAO,CAAC,CAAA;IAE1C,IAAI;AACF;MACA,MAAMI,gBAAgB,GAAGH,mBAAmB,CAACC,KAAK,EAAEF,OAAO,EAAEG,KAAK,CAAC,CAAA;;AAEnE;MACA,IAAIC,gBAAgB,CAACI,MAAM,EAAE;AAC3B4B,QAAAA,uBAAuB,CAAClC,KAAK,EAAEE,gBAAgB,EAAED,KAAK,CAAC,CAAA;AACzD,OAAA;MAEA8C,YAAY,CAACD,IAAI,CAAC,CAAA;KACnB,CAAC,OAAOE,GAAG,EAAE;AACZ/C,MAAAA,KAAK,CAAC,wCAAwC,EAAE+C,GAAG,CAACR,OAAO,CAAC,CAAA;AAC5DO,MAAAA,YAAY,CAAC,MAAMD,IAAI,CAACE,GAAG,CAAC,CAAC,CAAA;AAC/B,KAAA;GACD,CAAA;AACH;;;;"}
package/lib/index.js CHANGED
@@ -25,7 +25,10 @@ const defaults = {
25
25
  * @returns {Options} Normalized options
26
26
  */
27
27
  function normalizeOptions(options) {
28
- return Object.assign({}, defaults, options || {});
28
+ return {
29
+ ...defaults,
30
+ ...(options || {})
31
+ };
29
32
  }
30
33
 
31
34
  /**
@@ -173,10 +176,5 @@ function initMarkdownPartials(options) {
173
176
  };
174
177
  }
175
178
 
176
- // CommonJS export compatibility
177
- if (typeof module !== 'undefined') {
178
- module.exports = initMarkdownPartials;
179
- }
180
-
181
179
  export { initMarkdownPartials as default };
182
180
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["/**\n * A Metalsmith plugin to merge markdown partials into main markdown files.\n *\n * @module metalsmith-markdown-partials\n */\n\n/**\n * @typedef {Object} Options\n * @property {String} libraryPath - Path to the markdown partials library (defaults to './src/content/md-library/')\n * @property {String} fileSuffix - File suffix for markdown files (defaults to '.md')\n */\n\n// Define debug namespace at the top of the file\nconst debugNs = 'metalsmith-markdown-partials';\n\n/** @type {Options} */\nconst defaults = {\n libraryPath: './src/content/md-library/',\n fileSuffix: '.md'\n};\n\n/**\n * Normalize plugin options by merging with defaults\n * @param {Options} [options] - User provided options\n * @returns {Options} Normalized options\n */\nfunction normalizeOptions(options) {\n return Object.assign({}, defaults, options || {});\n}\n\n/**\n * Extract markdown partials from files and prepare them for insertion\n *\n * @param {Object} files - The metalsmith file object\n * @param {Options} options - Plugin options\n * @param {Function} debug - Debug function\n * @returns {Array} Array with all markdown include objects\n */\nfunction getMarkdownIncludes(files, options, debug) {\n const markdownIncludes = [];\n\n // Extract the library name from the path\n const libraryPath = options.libraryPath.slice(0, -1).split('/');\n const libraryName = libraryPath[libraryPath.length - 1];\n\n debug('Processing markdown files with libraryName: %s', libraryName);\n\n // Regex for matching include markers\n const markerRegex = /\\{#md\\s*\".+?\"\\s*#\\}/g;\n const markerStart = '{#md';\n const markerEnd = '#}';\n\n // Set to track already processed partials to prevent duplicates\n const processedPartials = new Set();\n\n Object.keys(files).forEach((file) => {\n /*\n * checks if string 'file' ends with options.fileSuffix\n * when metalsmith-in-place or metalsmith-layouts are used\n * the suffix depends on what templating language is used\n * for example with Nunjucks it would be .md.njk\n *\n * Also check that file does NOT start with libraryName as\n * the markdown partials library is also located in the content folder\n */\n if (file.endsWith(options.fileSuffix) && !file.startsWith(libraryName)) {\n const str = files[file].contents.toString();\n\n // Check if markers are present\n const matches = str.match(markerRegex);\n if (!matches) {return;}\n\n debug('Found %d markdown partials in %s', matches.length, file);\n\n // Process each marker in the file\n matches.forEach((marker) => {\n // Extract the filename from the marker\n const markerFileName = marker.replaceAll(' ', '').replace(`${markerStart}\"`, '').replace(`\"${markerEnd}`, '');\n const partialKey = `${libraryName}/${markerFileName}`;\n\n // Check if partial file exists\n if (!files[partialKey]) {\n debug('Warning: Partial file not found: %s', partialKey);\n return;\n }\n\n // Skip if we've already processed this exact marker+file combination\n const combinedKey = `${file}:${marker}`;\n if (processedPartials.has(combinedKey)) {return;}\n\n processedPartials.add(combinedKey);\n\n // Get the replacement content\n const replacementString = files[partialKey].contents.toString();\n\n markdownIncludes.push({\n marker,\n markerReplacement: replacementString,\n file\n });\n });\n }\n });\n\n // Remove markdown-partials from metalsmith build process\n Object.keys(files).forEach((file) => {\n if (file.startsWith(libraryName)) {\n delete files[file];\n }\n });\n\n debug('Processed %d markdown includes', markdownIncludes.length);\n return markdownIncludes;\n}\n\n/**\n * Replace markers with their markdown replacement strings\n *\n * @param {Object} files - The metalsmith file object\n * @param {Array} markdownIncludes - Array with all markdown include objects\n * @param {Function} debug - Debug function\n * @return {void}\n */\nfunction resolveMarkdownIncludes(files, markdownIncludes, debug) {\n // replace all markers with their markdown replacements\n markdownIncludes.forEach((markdownInclude) => {\n const fileData = files[markdownInclude.file];\n\n // replace the include marker with the actual include file content\n try {\n const contents = fileData.contents.toString();\n fileData.contents = Buffer.from(contents.replace(markdownInclude.marker, markdownInclude.markerReplacement));\n debug('Replaced marker in %s', markdownInclude.file);\n } catch (e) {\n debug('Error replacing marker in %s: %s', markdownInclude.file, e.message);\n console.error(`Error replacing marker in ${markdownInclude.file}:`, e);\n }\n });\n}\n\n/**\n * A Metalsmith plugin to merge markdown partials into main markdown file\n *\n * A marker of the form {#md \"<file name>.md\" #} indicates where the partial\n * must be inserted and also provides the file name of the replacement markdown.\n *\n * @param {Options} [options] - Plugin options\n * @returns {import('metalsmith').Plugin} Metalsmith plugin function\n * @example\n * // In your metalsmith build:\n * .use(markdownPartials({\n * libraryPath: './src/content/md-partials/',\n * fileSuffix: '.md.njk'\n * }))\n */\nfunction initMarkdownPartials(options) {\n options = normalizeOptions(options);\n\n return function markdownPartials(files, metalsmith, done) {\n // Use metalsmith's debug method if available\n const debug = metalsmith.debug ? metalsmith.debug(debugNs) : () => {};\n debug('Running with options: %o', options);\n\n try {\n // Get all markdown includes\n const markdownIncludes = getMarkdownIncludes(files, options, debug);\n\n // Replace include markers with their respective replacement\n if (markdownIncludes.length) {\n resolveMarkdownIncludes(files, markdownIncludes, debug);\n }\n\n setImmediate(done);\n } catch (err) {\n debug('Error processing markdown partials: %s', err.message);\n setImmediate(() => done(err));\n }\n };\n}\n\n// ESM export\nexport default initMarkdownPartials;\n\n// CommonJS export compatibility\nif (typeof module !== 'undefined') {\n module.exports = initMarkdownPartials;\n}\n"],"names":["debugNs","defaults","libraryPath","fileSuffix","normalizeOptions","options","Object","assign","getMarkdownIncludes","files","debug","markdownIncludes","slice","split","libraryName","length","markerRegex","markerStart","markerEnd","processedPartials","Set","keys","forEach","file","endsWith","startsWith","str","contents","toString","matches","match","marker","markerFileName","replaceAll","replace","partialKey","combinedKey","has","add","replacementString","push","markerReplacement","resolveMarkdownIncludes","markdownInclude","fileData","Buffer","from","e","message","console","error","initMarkdownPartials","markdownPartials","metalsmith","done","setImmediate","err","module","exports"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA,MAAMA,OAAO,GAAG,8BAA8B,CAAA;;AAE9C;AACA,MAAMC,QAAQ,GAAG;AACfC,EAAAA,WAAW,EAAE,2BAA2B;AACxCC,EAAAA,UAAU,EAAE,KAAA;AACd,CAAC,CAAA;;AAED;AACA;AACA;AACA;AACA;AACA,SAASC,gBAAgBA,CAACC,O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ACA;AACA;AACA;AACA,SAASI,oBAAoBA,CAAC9C,OAAO,EAAE;AACrCA,EAAAA,OAAO,GAAGD,gBAAgB,CAACC,OAAO,CAAC,CAAA;EAEnC,OAAO,SAAS+C,gBAAgBA,CAAC3C,KAAK,EAAE4C,UAAU,EAAEC,IAAI,EAAE;AACxD;AACA,IAAA,MAAM5C,KAAK,GAAG2C,UAAU,CAAC3C,KAAK,GAAG2C,UAAU,CAAC3C,KAAK,CAACV,OAAO,CAAC,GAAG,MAAM,EAAE,CAAA;AACrEU,IAAAA,KAAK,CAAC,0BAA0B,EAAEL,OAAO,CAAC,CAAA;IAE1C,IAAI;AACF;MACA,MAAMM,gBAAgB,GAAGH,mBAAmB,CAACC,KAAK,EAAEJ,OAAO,EAAEK,KAAK,CAAC,CAAA;;AAEnE;MACA,IAAIC,gBAAgB,CAACI,MAAM,EAAE;AAC3B2B,QAAAA,uBAAuB,CAACjC,KAAK,EAAEE,gBAAgB,EAAED,KAAK,CAAC,CAAA;AACzD,OAAA;MAEA6C,YAAY,CAACD,IAAI,CAAC,CAAA;KACnB,CAAC,OAAOE,GAAG,EAAE;AACZ9C,MAAAA,KAAK,CAAC,wCAAwC,EAAE8C,GAAG,CAACR,OAAO,CAAC,CAAA;AAC5DO,MAAAA,YAAY,CAAC,MAAMD,IAAI,CAACE,GAAG,CAAC,CAAC,CAAA;AAC/B,KAAA;GACD,CAAA;AACH,CAAA;;AAKA;AACA,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE;EACjCA,MAAM,CAACC,OAAO,GAAGP,oBAAoB,CAAA;AACvC;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["/**\n * A Metalsmith plugin to merge markdown partials into main markdown files.\n *\n * @module metalsmith-markdown-partials\n */\n\n/**\n * @typedef {Object} Options\n * @property {String} libraryPath - Path to the markdown partials library (defaults to './src/content/md-library/')\n * @property {String} fileSuffix - File suffix for markdown files (defaults to '.md')\n */\n\n// Define debug namespace at the top of the file\nconst debugNs = 'metalsmith-markdown-partials';\n\n/** @type {Options} */\nconst defaults = {\n libraryPath: './src/content/md-library/',\n fileSuffix: '.md'\n};\n\n/**\n * Normalize plugin options by merging with defaults\n * @param {Options} [options] - User provided options\n * @returns {Options} Normalized options\n */\nfunction normalizeOptions(options) {\n return { ...defaults, ...(options || {}) };\n}\n\n/**\n * Extract markdown partials from files and prepare them for insertion\n *\n * @param {Object} files - The metalsmith file object\n * @param {Options} options - Plugin options\n * @param {Function} debug - Debug function\n * @returns {Array} Array with all markdown include objects\n */\nfunction getMarkdownIncludes(files, options, debug) {\n const markdownIncludes = [];\n\n // Extract the library name from the path\n const libraryPath = options.libraryPath.slice(0, -1).split('/');\n const libraryName = libraryPath[libraryPath.length - 1];\n\n debug('Processing markdown files with libraryName: %s', libraryName);\n\n // Regex for matching include markers\n const markerRegex = /\\{#md\\s*\".+?\"\\s*#\\}/g;\n const markerStart = '{#md';\n const markerEnd = '#}';\n\n // Set to track already processed partials to prevent duplicates\n const processedPartials = new Set();\n\n Object.keys(files).forEach((file) => {\n /*\n * checks if string 'file' ends with options.fileSuffix\n * when metalsmith-in-place or metalsmith-layouts are used\n * the suffix depends on what templating language is used\n * for example with Nunjucks it would be .md.njk\n *\n * Also check that file does NOT start with libraryName as\n * the markdown partials library is also located in the content folder\n */\n if (file.endsWith(options.fileSuffix) && !file.startsWith(libraryName)) {\n const str = files[file].contents.toString();\n\n // Check if markers are present\n const matches = str.match(markerRegex);\n if (!matches) {\n return;\n }\n\n debug('Found %d markdown partials in %s', matches.length, file);\n\n // Process each marker in the file\n matches.forEach((marker) => {\n // Extract the filename from the marker\n const markerFileName = marker.replaceAll(' ', '').replace(`${markerStart}\"`, '').replace(`\"${markerEnd}`, '');\n const partialKey = `${libraryName}/${markerFileName}`;\n\n // Check if partial file exists\n if (!files[partialKey]) {\n debug('Warning: Partial file not found: %s', partialKey);\n return;\n }\n\n // Skip if we've already processed this exact marker+file combination\n const combinedKey = `${file}:${marker}`;\n if (processedPartials.has(combinedKey)) {\n return;\n }\n\n processedPartials.add(combinedKey);\n\n // Get the replacement content\n const replacementString = files[partialKey].contents.toString();\n\n markdownIncludes.push({\n marker,\n markerReplacement: replacementString,\n file\n });\n });\n }\n });\n\n // Remove markdown-partials from metalsmith build process\n Object.keys(files).forEach((file) => {\n if (file.startsWith(libraryName)) {\n delete files[file];\n }\n });\n\n debug('Processed %d markdown includes', markdownIncludes.length);\n return markdownIncludes;\n}\n\n/**\n * Replace markers with their markdown replacement strings\n *\n * @param {Object} files - The metalsmith file object\n * @param {Array} markdownIncludes - Array with all markdown include objects\n * @param {Function} debug - Debug function\n * @return {void}\n */\nfunction resolveMarkdownIncludes(files, markdownIncludes, debug) {\n // replace all markers with their markdown replacements\n markdownIncludes.forEach((markdownInclude) => {\n const fileData = files[markdownInclude.file];\n\n // replace the include marker with the actual include file content\n try {\n const contents = fileData.contents.toString();\n fileData.contents = Buffer.from(contents.replace(markdownInclude.marker, markdownInclude.markerReplacement));\n debug('Replaced marker in %s', markdownInclude.file);\n } catch (e) {\n debug('Error replacing marker in %s: %s', markdownInclude.file, e.message);\n console.error(`Error replacing marker in ${markdownInclude.file}:`, e);\n }\n });\n}\n\n/**\n * A Metalsmith plugin to merge markdown partials into main markdown file\n *\n * A marker of the form {#md \"<file name>.md\" #} indicates where the partial\n * must be inserted and also provides the file name of the replacement markdown.\n *\n * @param {Options} [options] - Plugin options\n * @returns {import('metalsmith').Plugin} Metalsmith plugin function\n * @example\n * // In your metalsmith build:\n * .use(markdownPartials({\n * libraryPath: './src/content/md-partials/',\n * fileSuffix: '.md.njk'\n * }))\n */\nfunction initMarkdownPartials(options) {\n options = normalizeOptions(options);\n\n return function markdownPartials(files, metalsmith, done) {\n // Use metalsmith's debug method if available\n const debug = metalsmith.debug ? metalsmith.debug(debugNs) : () => {};\n debug('Running with options: %o', options);\n\n try {\n // Get all markdown includes\n const markdownIncludes = getMarkdownIncludes(files, options, debug);\n\n // Replace include markers with their respective replacement\n if (markdownIncludes.length) {\n resolveMarkdownIncludes(files, markdownIncludes, debug);\n }\n\n setImmediate(done);\n } catch (err) {\n debug('Error processing markdown partials: %s', err.message);\n setImmediate(() => done(err));\n }\n };\n}\n\n// ESM export\nexport default initMarkdownPartials;\n"],"names":["debugNs","defaults","libraryPath","fileSuffix","normalizeOptions","options","getMarkdownIncludes","files","debug","markdownIncludes","slice","split","libraryName","length","markerRegex","markerStart","markerEnd","processedPartials","Set","Object","keys","forEach","file","endsWith","startsWith","str","contents","toString","matches","match","marker","markerFileName","replaceAll","replace","partialKey","combinedKey","has","add","replacementString","push","markerReplacement","resolveMarkdownIncludes","markdownInclude","fileData","Buffer","from","e","message","console","error","initMarkdownPartials","markdownPartials","metalsmith","done","setImmediate","err"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA,MAAMA,OAAO,GAAG,8BAA8B,CAAA;;AAE9C;AACA,MAAMC,QAAQ,GAAG;AACfC,EAAAA,WAAW,EAAE,2BAA2B;AACxCC,EAAAA,UAAU,EAAE,KAAA;AACd,CAAC,CAAA;;AAED;AACA;AACA;AACA;AACA;AACA,SAASC,gBAAgBA,CAACC,OAAO,EAAE;EACjC,OAAO;AAAE,IAAA,GAAGJ,QAAQ;IAAE,IAAII,OAAO,IAAI,EAAE,CAAA;GAAG,CAAA;AAC5C,CAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,mBAAmBA,CAACC,KAAK,EAAEF,OAAO,EAAEG,KAAK,EAAE;EAClD,MAAMC,gBAAgB,GAAG,EAAE,CAAA;;AAE3B;AACA,EAAA,MAAMP,WAAW,GAAGG,OAAO,CAACH,WAAW,CAACQ,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAACC,KAAK,CAAC,GAAG,CAAC,CAAA;EAC/D,MAAMC,WAAW,GAAGV,WAAW,CAACA,WAAW,CAACW,MAAM,GAAG,CAAC,CAAC,CAAA;AAEvDL,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B,CAACI,MAAM,EAAE;AAC3B4B,QAAAA,uBAAuB,CAAClC,KAAK,EAAEE,gBAAgB,EAAED,KAAK,CAAC,CAAA;AACzD,OAAA;MAEA8C,YAAY,CAACD,IAAI,CAAC,CAAA;KACnB,CAAC,OAAOE,GAAG,EAAE;AACZ/C,MAAAA,KAAK,CAAC,wCAAwC,EAAE+C,GAAG,CAACR,OAAO,CAAC,CAAA;AAC5DO,MAAAA,YAAY,CAAC,MAAMD,IAAI,CAACE,GAAG,CAAC,CAAC,CAAA;AAC/B,KAAA;GACD,CAAA;AACH;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metalsmith-markdown-partials",
3
- "version": "2.5.1",
3
+ "version": "2.5.4",
4
4
  "description": "A Metalsmith plugin that allows the use of partial markdown files",
5
5
  "keywords": [
6
6
  "metalsmith-plugin",
@@ -22,7 +22,6 @@
22
22
  "url": "git+https://github.com/wernerglinka/metalsmith-markdown-partials.git"
23
23
  },
24
24
  "files": [
25
- "src",
26
25
  "lib",
27
26
  "LICENSE",
28
27
  "README.md"
@@ -45,26 +44,25 @@
45
44
  "lint": "eslint --fix .",
46
45
  "lint:check": "eslint --fix-dry-run .",
47
46
  "prepublishOnly": "npm run build",
48
- "release:patch": "release-it patch",
49
- "release:minor": "release-it minor",
50
- "release:major": "release-it major",
47
+ "release:patch": "./scripts/release.sh patch --ci",
48
+ "release:minor": "./scripts/release.sh minor --ci",
49
+ "release:major": "./scripts/release.sh major --ci",
51
50
  "release:check": "npm run lint:check && npm run build && release-it --dry-run",
52
51
  "test": "c8 --include=src/**/*.js mocha 'test/index.js' 'test/cjs.test.cjs' -t 15000",
53
52
  "test:esm": "c8 --include=src/**/*.js mocha test/index.js -t 15000",
54
53
  "test:cjs": "c8 --include=src/**/*.js mocha test/cjs.test.cjs -t 15000",
55
- "test:e2e": "serve -l 3000 test/fixtures",
56
- "depcheck": "depcheck"
54
+ "test:e2e": "serve -l 3000 test/fixtures"
57
55
  },
58
56
  "devDependencies": {
59
57
  "auto-changelog": "^2.5.0",
60
58
  "c8": "^10.1.3",
61
- "eslint": "^9.32.0",
59
+ "eslint": "^10.0.0",
62
60
  "eslint-config-prettier": "^10.1.8",
63
- "metalsmith": "^2.6.3",
61
+ "metalsmith": "^2.7.0",
64
62
  "microbundle": "^0.15.1",
65
- "mocha": "^11.7.1",
66
- "prettier": "^3.6.2",
67
- "release-it": "19.0.4"
63
+ "mocha": "^11.7.5",
64
+ "prettier": "^3.8.1",
65
+ "release-it": "19.2.4"
68
66
  },
69
67
  "peerDependencies": {
70
68
  "metalsmith": "^2.5.1"
@@ -74,8 +72,5 @@
74
72
  },
75
73
  "publishConfig": {
76
74
  "access": "public"
77
- },
78
- "dependencies": {
79
- "depcheck": "^1.4.7"
80
75
  }
81
76
  }
package/src/index.js DELETED
@@ -1,187 +0,0 @@
1
- /**
2
- * A Metalsmith plugin to merge markdown partials into main markdown files.
3
- *
4
- * @module metalsmith-markdown-partials
5
- */
6
-
7
- /**
8
- * @typedef {Object} Options
9
- * @property {String} libraryPath - Path to the markdown partials library (defaults to './src/content/md-library/')
10
- * @property {String} fileSuffix - File suffix for markdown files (defaults to '.md')
11
- */
12
-
13
- // Define debug namespace at the top of the file
14
- const debugNs = 'metalsmith-markdown-partials';
15
-
16
- /** @type {Options} */
17
- const defaults = {
18
- libraryPath: './src/content/md-library/',
19
- fileSuffix: '.md'
20
- };
21
-
22
- /**
23
- * Normalize plugin options by merging with defaults
24
- * @param {Options} [options] - User provided options
25
- * @returns {Options} Normalized options
26
- */
27
- function normalizeOptions(options) {
28
- return Object.assign({}, defaults, options || {});
29
- }
30
-
31
- /**
32
- * Extract markdown partials from files and prepare them for insertion
33
- *
34
- * @param {Object} files - The metalsmith file object
35
- * @param {Options} options - Plugin options
36
- * @param {Function} debug - Debug function
37
- * @returns {Array} Array with all markdown include objects
38
- */
39
- function getMarkdownIncludes(files, options, debug) {
40
- const markdownIncludes = [];
41
-
42
- // Extract the library name from the path
43
- const libraryPath = options.libraryPath.slice(0, -1).split('/');
44
- const libraryName = libraryPath[libraryPath.length - 1];
45
-
46
- debug('Processing markdown files with libraryName: %s', libraryName);
47
-
48
- // Regex for matching include markers
49
- const markerRegex = /\{#md\s*".+?"\s*#\}/g;
50
- const markerStart = '{#md';
51
- const markerEnd = '#}';
52
-
53
- // Set to track already processed partials to prevent duplicates
54
- const processedPartials = new Set();
55
-
56
- Object.keys(files).forEach((file) => {
57
- /*
58
- * checks if string 'file' ends with options.fileSuffix
59
- * when metalsmith-in-place or metalsmith-layouts are used
60
- * the suffix depends on what templating language is used
61
- * for example with Nunjucks it would be .md.njk
62
- *
63
- * Also check that file does NOT start with libraryName as
64
- * the markdown partials library is also located in the content folder
65
- */
66
- if (file.endsWith(options.fileSuffix) && !file.startsWith(libraryName)) {
67
- const str = files[file].contents.toString();
68
-
69
- // Check if markers are present
70
- const matches = str.match(markerRegex);
71
- if (!matches) {return;}
72
-
73
- debug('Found %d markdown partials in %s', matches.length, file);
74
-
75
- // Process each marker in the file
76
- matches.forEach((marker) => {
77
- // Extract the filename from the marker
78
- const markerFileName = marker.replaceAll(' ', '').replace(`${markerStart}"`, '').replace(`"${markerEnd}`, '');
79
- const partialKey = `${libraryName}/${markerFileName}`;
80
-
81
- // Check if partial file exists
82
- if (!files[partialKey]) {
83
- debug('Warning: Partial file not found: %s', partialKey);
84
- return;
85
- }
86
-
87
- // Skip if we've already processed this exact marker+file combination
88
- const combinedKey = `${file}:${marker}`;
89
- if (processedPartials.has(combinedKey)) {return;}
90
-
91
- processedPartials.add(combinedKey);
92
-
93
- // Get the replacement content
94
- const replacementString = files[partialKey].contents.toString();
95
-
96
- markdownIncludes.push({
97
- marker,
98
- markerReplacement: replacementString,
99
- file
100
- });
101
- });
102
- }
103
- });
104
-
105
- // Remove markdown-partials from metalsmith build process
106
- Object.keys(files).forEach((file) => {
107
- if (file.startsWith(libraryName)) {
108
- delete files[file];
109
- }
110
- });
111
-
112
- debug('Processed %d markdown includes', markdownIncludes.length);
113
- return markdownIncludes;
114
- }
115
-
116
- /**
117
- * Replace markers with their markdown replacement strings
118
- *
119
- * @param {Object} files - The metalsmith file object
120
- * @param {Array} markdownIncludes - Array with all markdown include objects
121
- * @param {Function} debug - Debug function
122
- * @return {void}
123
- */
124
- function resolveMarkdownIncludes(files, markdownIncludes, debug) {
125
- // replace all markers with their markdown replacements
126
- markdownIncludes.forEach((markdownInclude) => {
127
- const fileData = files[markdownInclude.file];
128
-
129
- // replace the include marker with the actual include file content
130
- try {
131
- const contents = fileData.contents.toString();
132
- fileData.contents = Buffer.from(contents.replace(markdownInclude.marker, markdownInclude.markerReplacement));
133
- debug('Replaced marker in %s', markdownInclude.file);
134
- } catch (e) {
135
- debug('Error replacing marker in %s: %s', markdownInclude.file, e.message);
136
- console.error(`Error replacing marker in ${markdownInclude.file}:`, e);
137
- }
138
- });
139
- }
140
-
141
- /**
142
- * A Metalsmith plugin to merge markdown partials into main markdown file
143
- *
144
- * A marker of the form {#md "<file name>.md" #} indicates where the partial
145
- * must be inserted and also provides the file name of the replacement markdown.
146
- *
147
- * @param {Options} [options] - Plugin options
148
- * @returns {import('metalsmith').Plugin} Metalsmith plugin function
149
- * @example
150
- * // In your metalsmith build:
151
- * .use(markdownPartials({
152
- * libraryPath: './src/content/md-partials/',
153
- * fileSuffix: '.md.njk'
154
- * }))
155
- */
156
- function initMarkdownPartials(options) {
157
- options = normalizeOptions(options);
158
-
159
- return function markdownPartials(files, metalsmith, done) {
160
- // Use metalsmith's debug method if available
161
- const debug = metalsmith.debug ? metalsmith.debug(debugNs) : () => {};
162
- debug('Running with options: %o', options);
163
-
164
- try {
165
- // Get all markdown includes
166
- const markdownIncludes = getMarkdownIncludes(files, options, debug);
167
-
168
- // Replace include markers with their respective replacement
169
- if (markdownIncludes.length) {
170
- resolveMarkdownIncludes(files, markdownIncludes, debug);
171
- }
172
-
173
- setImmediate(done);
174
- } catch (err) {
175
- debug('Error processing markdown partials: %s', err.message);
176
- setImmediate(() => done(err));
177
- }
178
- };
179
- }
180
-
181
- // ESM export
182
- export default initMarkdownPartials;
183
-
184
- // CommonJS export compatibility
185
- if (typeof module !== 'undefined') {
186
- module.exports = initMarkdownPartials;
187
- }