metalsmith-markdown-partials 2.5.0 → 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 +7 -3
- package/lib/index.cjs +4 -6
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +4 -6
- package/lib/index.js.map +1 -1
- package/package.json +14 -18
package/README.md
CHANGED
|
@@ -7,9 +7,16 @@ A Metalsmith plugin that enables the use of Markdown partials.
|
|
|
7
7
|
[![license: MIT][license-badge]][license-url]
|
|
8
8
|
[![coverage][coverage-badge]][coverage-url]
|
|
9
9
|
[![ESM/CommonJS][modules-badge]][npm-url]
|
|
10
|
+
[](https://snyk.io/test/npm/metalsmith-markdown-partials)
|
|
10
11
|
|
|
11
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.
|
|
12
13
|
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- This plugin supports both ESM and CommonJS environments with no configuration needed:
|
|
17
|
+
- ESM: `import mdPartials from 'metalsmith-markdown-partials'`
|
|
18
|
+
- CommonJS: `const mdPartials = require('metalsmith-markdown-partials')`
|
|
19
|
+
|
|
13
20
|
## Installation
|
|
14
21
|
|
|
15
22
|
```js
|
|
@@ -19,9 +26,6 @@ $ npm install --save metalsmith-markdown-partials
|
|
|
19
26
|
## Usage
|
|
20
27
|
|
|
21
28
|
```js
|
|
22
|
-
var mdPartials = require('metalsmith-markdown-partials');
|
|
23
|
-
|
|
24
|
-
...
|
|
25
29
|
.use(mdPartials({
|
|
26
30
|
libraryPath: './markdown-partials/',
|
|
27
31
|
fileSuffix: '.md.njk',
|
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
|
|
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,MAAM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"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,MAAM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AAM,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
|
|
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,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;A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K,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,EAAAA,KAAK,CAAC,gDAAgD,EAAEI,WAAW,CAAC,CAAA;;AAEpE;EACA,MAAME,WAAW,GAAG,sBAAsB,CAAA;EAC1C,MAAMC,WAAW,GAAG,MAAM,CAAA;EAC1B,MAAMC,SAAS,GAAG,IAAI,CAAA;;AAEtB;AACA,EAAA,MAAMC,iBAAiB,GAAG,IAAIC,GAAG,EAAE,CAAA;EAEnCC,MAAM,CAACC,IAAI,CAACb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AY,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.
|
|
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",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"lib",
|
|
26
|
-
"LICENSE
|
|
26
|
+
"LICENSE",
|
|
27
27
|
"README.md"
|
|
28
28
|
],
|
|
29
29
|
"author": {
|
|
@@ -38,32 +38,31 @@
|
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "microbundle --entry src/index.js --output lib/index.js --target node -f esm,cjs --strict --generateTypes=false",
|
|
40
40
|
"changelog": "auto-changelog -u --commit-limit false --ignore-commit-pattern '^((dev|chore|ci):|Release)'",
|
|
41
|
-
"coverage": "
|
|
41
|
+
"coverage": "c8 --include=src/**/*.js --reporter=lcov --reporter=text-summary mocha 'test/index.js' 'test/cjs.test.cjs' -t 15000",
|
|
42
42
|
"format": "prettier --write \"**/*.{yml,md,js,json}\"",
|
|
43
43
|
"format:check": "prettier --list-different \"**/*.{yml,md,js,json}\"",
|
|
44
44
|
"lint": "eslint --fix .",
|
|
45
45
|
"lint:check": "eslint --fix-dry-run .",
|
|
46
46
|
"prepublishOnly": "npm run build",
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"release": "
|
|
50
|
-
"release:check": "npm run lint:check && npm run build &&
|
|
47
|
+
"release:patch": "./scripts/release.sh patch --ci",
|
|
48
|
+
"release:minor": "./scripts/release.sh minor --ci",
|
|
49
|
+
"release:major": "./scripts/release.sh major --ci",
|
|
50
|
+
"release:check": "npm run lint:check && npm run build && release-it --dry-run",
|
|
51
51
|
"test": "c8 --include=src/**/*.js mocha 'test/index.js' 'test/cjs.test.cjs' -t 15000",
|
|
52
52
|
"test:esm": "c8 --include=src/**/*.js mocha test/index.js -t 15000",
|
|
53
53
|
"test:cjs": "c8 --include=src/**/*.js mocha test/cjs.test.cjs -t 15000",
|
|
54
|
-
"test:e2e": "serve -l 3000 test/fixtures"
|
|
55
|
-
"depcheck": "depcheck"
|
|
54
|
+
"test:e2e": "serve -l 3000 test/fixtures"
|
|
56
55
|
},
|
|
57
56
|
"devDependencies": {
|
|
58
57
|
"auto-changelog": "^2.5.0",
|
|
59
58
|
"c8": "^10.1.3",
|
|
60
|
-
"eslint": "^
|
|
61
|
-
"eslint-config-prettier": "^10.1.
|
|
62
|
-
"metalsmith": "^2.
|
|
59
|
+
"eslint": "^10.0.0",
|
|
60
|
+
"eslint-config-prettier": "^10.1.8",
|
|
61
|
+
"metalsmith": "^2.7.0",
|
|
63
62
|
"microbundle": "^0.15.1",
|
|
64
|
-
"mocha": "^11.
|
|
65
|
-
"prettier": "^3.
|
|
66
|
-
"release-it": "19.
|
|
63
|
+
"mocha": "^11.7.5",
|
|
64
|
+
"prettier": "^3.8.1",
|
|
65
|
+
"release-it": "19.2.4"
|
|
67
66
|
},
|
|
68
67
|
"peerDependencies": {
|
|
69
68
|
"metalsmith": "^2.5.1"
|
|
@@ -73,8 +72,5 @@
|
|
|
73
72
|
},
|
|
74
73
|
"publishConfig": {
|
|
75
74
|
"access": "public"
|
|
76
|
-
},
|
|
77
|
-
"dependencies": {
|
|
78
|
-
"depcheck": "^1.4.7"
|
|
79
75
|
}
|
|
80
76
|
}
|