metalsmith-markdown-partials 2.0.0 → 2.0.1
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/lib/index.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
2
|
* @typedef Options
|
|
6
3
|
* @property {String} key
|
|
@@ -8,7 +5,7 @@ const fs = require('fs');
|
|
|
8
5
|
|
|
9
6
|
/** @type {Options} */
|
|
10
7
|
const defaults = {
|
|
11
|
-
libraryPath: './src/md-library/',
|
|
8
|
+
libraryPath: './src/content/md-library/',
|
|
12
9
|
fileSuffix: '.md'
|
|
13
10
|
};
|
|
14
11
|
|
|
@@ -17,96 +14,100 @@ const defaults = {
|
|
|
17
14
|
* @param {Options} [options]
|
|
18
15
|
* @returns {Object}
|
|
19
16
|
*/
|
|
20
|
-
function normalizeOptions(options) {
|
|
17
|
+
function normalizeOptions(options) {
|
|
21
18
|
return Object.assign({}, defaults, options || {});
|
|
22
19
|
}
|
|
23
20
|
|
|
24
21
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
22
|
+
* getMarkdownIncludes
|
|
23
|
+
* Get include markdown markers and replacements
|
|
27
24
|
*
|
|
28
|
-
* @param {[
|
|
29
|
-
* @
|
|
25
|
+
* @param {[Object]} files - the metalsmith file object
|
|
26
|
+
* @param {Options} options
|
|
27
|
+
*
|
|
28
|
+
* @return {[Array]} Array with all markdown include objects
|
|
30
29
|
*/
|
|
30
|
+
function getMarkdownIncludes(files, options) {
|
|
31
|
+
const markdownIncludes = [];
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
33
|
+
// get the library name
|
|
34
|
+
const libraryPath = options.libraryPath.slice(0, -1).split("/");
|
|
35
|
+
const libraryName = libraryPath[libraryPath.length - 1];
|
|
36
|
+
|
|
37
|
+
Object.keys(files).forEach(function (file) {
|
|
38
|
+
/*
|
|
39
|
+
* checks if string 'file' ends with options.fileSuffix
|
|
40
|
+
* when metalsmith-in-place or metalsmith-layouts are used
|
|
41
|
+
* the suffix depends on what templating language is used
|
|
42
|
+
* for example with Nunjucks it would be .md.njk
|
|
43
|
+
*
|
|
44
|
+
* Also check that file does NOT start with libraryName as
|
|
45
|
+
* the markdown partials library is also located in the content
|
|
46
|
+
* folder
|
|
47
|
+
*/
|
|
48
|
+
if (file.endsWith(options.fileSuffix) && !file.startsWith(libraryName)) {
|
|
49
|
+
const markerStart = "{#md";
|
|
50
|
+
const markerEnd = "#}";
|
|
51
|
+
const str = files[file].contents.toString();
|
|
52
|
+
|
|
53
|
+
// check for markers present
|
|
54
|
+
const count = str.match(/\{#md\s*".+?"\s*#\}/g).length;
|
|
55
|
+
|
|
56
|
+
// get all markdown includes
|
|
57
|
+
if (count) {
|
|
58
|
+
for (let i = 0; count > i; i++) {
|
|
59
|
+
const marker = str.match(/\{#md\s*"(.+?)"\s*#\}/g)[i];
|
|
60
|
+
const markerFileName = marker.replaceAll(" ", "").replace(`${markerStart}"`, "").replace(`"${markerEnd}`, "");
|
|
61
|
+
|
|
62
|
+
// get the replacement markdown string
|
|
63
|
+
// by reconstructing the object key for the replacement markdown file
|
|
64
|
+
// `${libraryName}/${markerFileName}`
|
|
65
|
+
replacementString = files[`${libraryName}/${markerFileName}`].contents.toString();
|
|
66
|
+
|
|
67
|
+
markdownIncludes.push({
|
|
68
|
+
marker,
|
|
69
|
+
markerReplacement: replacementString,
|
|
70
|
+
file
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Remove markdown-partials from metalsmith build process
|
|
79
|
+
Object.keys(files).forEach(function (file) {
|
|
80
|
+
if (file.startsWith(libraryName)) {
|
|
81
|
+
delete files[file];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return markdownIncludes
|
|
66
86
|
}
|
|
67
87
|
|
|
68
88
|
/**
|
|
69
|
-
*
|
|
70
|
-
* Replace
|
|
89
|
+
* resolveMarkdownIncludes
|
|
90
|
+
* Replace markers with their markdown replacement strings
|
|
71
91
|
*
|
|
72
|
-
* @param {[
|
|
73
|
-
* @param {[
|
|
74
|
-
* @param {[object]} options - plugin options. in this case the path to the directory that holds all the markdown includes
|
|
92
|
+
* @param {[Object]} files - the metalsmith file object
|
|
93
|
+
* @param {[Array]} markdownIncludes with all markdown include objects
|
|
75
94
|
* @return {[void]}
|
|
76
95
|
*/
|
|
96
|
+
function resolveMarkdownIncludes(files, markdownIncludes) {
|
|
97
|
+
// replace all markers with their markdown replacements
|
|
98
|
+
markdownIncludes.forEach(function(markdownInclude) {
|
|
77
99
|
|
|
78
|
-
|
|
79
|
-
let markerString, filePath, replaceThis;
|
|
80
|
-
const str = fileData.contents.toString();
|
|
81
|
-
let data;
|
|
82
|
-
|
|
83
|
-
for (i = 0; markers.length > i; i++) {
|
|
84
|
-
// get the whole marker string
|
|
85
|
-
markerString = str.substring(markers[i][0], markers[i][1]);
|
|
86
|
-
|
|
87
|
-
// extract the markdown particle file path from the marker string
|
|
88
|
-
// replace single quotes with double quotes... just in case
|
|
89
|
-
filePath = markerString.replace(/'/g, '"');
|
|
90
|
-
// get the path inside the marker between the quotes
|
|
91
|
-
filePath = filePath.match(/"([^"]+)"/)[1];
|
|
92
|
-
|
|
93
|
-
// get the file content
|
|
94
|
-
const libPath = options.libraryPath;
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
data = fs.readFileSync(libPath + filePath, 'utf8');
|
|
98
|
-
} catch (e) {
|
|
99
|
-
console.log('Error:', e.stack);
|
|
100
|
-
}
|
|
100
|
+
const fileData = files[markdownInclude.file];
|
|
101
101
|
|
|
102
102
|
// replace the include marker with the actual include file content
|
|
103
103
|
try {
|
|
104
104
|
const contents = fileData.contents.toString();
|
|
105
|
-
fileData.contents = Buffer.from(contents.replace(
|
|
105
|
+
fileData.contents = Buffer.from(contents.replace(markdownInclude.marker, markdownInclude.markerReplacement));
|
|
106
106
|
} catch (e) {
|
|
107
107
|
console.error(e);
|
|
108
108
|
}
|
|
109
|
-
|
|
109
|
+
|
|
110
|
+
});
|
|
110
111
|
}
|
|
111
112
|
|
|
112
113
|
/**
|
|
@@ -119,31 +120,21 @@ function resolveIncludes(fileData, markers, options) {
|
|
|
119
120
|
* @returns {import('metalsmith').Plugin}
|
|
120
121
|
*/
|
|
121
122
|
|
|
122
|
-
function
|
|
123
|
+
function initMarkdownPartials(options) {
|
|
123
124
|
options = normalizeOptions(options);
|
|
124
125
|
|
|
125
|
-
return function (files, metalsmith, done) {
|
|
126
|
+
return function markdownPartials(files, metalsmith, done) {
|
|
126
127
|
setImmediate(done);
|
|
127
128
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
// get include markers for this file
|
|
138
|
-
const markers = getIncludes(fileData);
|
|
139
|
-
|
|
140
|
-
// replace include markers with their respective replacement
|
|
141
|
-
if (markers.length) {
|
|
142
|
-
resolveIncludes(fileData, markers, options);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
});
|
|
129
|
+
// get all markdown includes
|
|
130
|
+
const markdownIncludes = getMarkdownIncludes(files, options);
|
|
131
|
+
|
|
132
|
+
// replace include markers with their respective replacement
|
|
133
|
+
if (markdownIncludes.length) {
|
|
134
|
+
resolveMarkdownIncludes(files, markdownIncludes);
|
|
135
|
+
}
|
|
136
|
+
|
|
146
137
|
};
|
|
147
138
|
}
|
|
148
139
|
|
|
149
|
-
module.exports =
|
|
140
|
+
module.exports = initMarkdownPartials;
|
package/package.json
CHANGED
|
File without changes
|
package/tests/index.js
CHANGED
|
@@ -21,7 +21,7 @@ describe('metalsmith-markdown-partials', () => {
|
|
|
21
21
|
|
|
22
22
|
metalsmith(fixture())
|
|
23
23
|
.use(mdPartials({
|
|
24
|
-
libraryPath: path.join(fixture(), `/md-partials/`),
|
|
24
|
+
libraryPath: path.join(fixture(), `/src/md-partials/`),
|
|
25
25
|
fileSuffix: '.md',
|
|
26
26
|
}))
|
|
27
27
|
.build( err => {
|