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
- * getIncludes
26
- * Extract include markers from file contents
22
+ * getMarkdownIncludes
23
+ * Get include markdown markers and replacements
27
24
  *
28
- * @param {[string]} str - the file contents in string form
29
- * @return {[array]} markers - the start and end indexes for all include markers
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
- function getIncludes(fileData) {
33
- const str = fileData.contents.toString();
34
- const markers = [];
35
- let previousMarker, i;
36
- let temp = [];
37
-
38
- // find first include marker
39
- let newMarker = str.indexOf('{#md');
40
-
41
- if (newMarker > -1) {
42
- // get the number of partials in this file
43
- const count = (str.match(/{#md/g) || []).length;
44
-
45
- // get the marker boundaries for this file
46
- // a marker looks like this: {#md "<partial name>.md"#}
47
- for (i = 0; count > i; i++) {
48
- // get the marker pair
49
- temp.push(newMarker);
50
- previousMarker = newMarker;
51
-
52
- // find the closing brackets '#}'
53
- newMarker = str.indexOf('#}', previousMarker) + 2;
54
- temp.push(newMarker);
55
-
56
- // push marker pair into markers array
57
- markers.push(temp);
58
-
59
- temp = [];
60
- // find the next marker
61
- previousMarker = newMarker;
62
- newMarker = str.indexOf('{#md', previousMarker);
63
- }
64
- }
65
- return markers;
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
- * resolveIncludes
70
- * Replace include markers with their respective files content
89
+ * resolveMarkdownIncludes
90
+ * Replace markers with their markdown replacement strings
71
91
  *
72
- * @param {[object]} fileData - the file being processed as an object
73
- * @param {[array]} markers - the start and end indexes for all include markers
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
- function resolveIncludes(fileData, markers, options) {
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(markerString, data));
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 markdownPartials(options) {
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
- Object.keys(files).forEach(function (file) {
129
- // checks if string 'file' ends with options.fileSuffix
130
- // when metalsmith-in-place or metalsmith-layouts are used
131
- // the suffix depends on what templating language is used
132
- // for example with Nunjucks it would be .md.njk
133
- if (file.endsWith(options.fileSuffix)) {
134
- // get the file data
135
- const fileData = files[file];
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 = markdownPartials;
140
+ module.exports = initMarkdownPartials;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metalsmith-markdown-partials",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "A Metalsmith plugin that allows the use of partial markdowm files",
5
5
  "keywords": [
6
6
  "metalsmith-plugin",
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 => {