metalsmith-markdown-partials 2.0.0 → 2.0.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/.eslintcache CHANGED
@@ -1 +1 @@
1
- [{"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/lib/index.js":"1"},{"size":4168,"mtime":1643755486658,"results":"2","hashOfConfig":"3"},{"filePath":"4","messages":"5","suppressedMessages":"6","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"uajcbk","/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/lib/index.js",[],["7","8","9","10","11","12"],{"ruleId":"13","severity":2,"message":"14","line":79,"column":31,"nodeType":"15","messageId":"16","endLine":79,"endColumn":42,"suppressions":"17"},{"ruleId":"18","severity":2,"message":"19","line":83,"column":8,"nodeType":"15","messageId":"20","endLine":83,"endColumn":9,"suppressions":"21"},{"ruleId":"18","severity":2,"message":"19","line":83,"column":32,"nodeType":"15","messageId":"20","endLine":83,"endColumn":33,"suppressions":"22"},{"ruleId":"18","severity":2,"message":"19","line":83,"column":35,"nodeType":"15","messageId":"20","endLine":83,"endColumn":36,"suppressions":"23"},{"ruleId":"18","severity":2,"message":"19","line":85,"column":42,"nodeType":"15","messageId":"20","endLine":85,"endColumn":43,"suppressions":"24"},{"ruleId":"18","severity":2,"message":"19","line":85,"column":57,"nodeType":"15","messageId":"20","endLine":85,"endColumn":58,"suppressions":"25"},"no-unused-vars","'replaceThis' is defined but never used.","Identifier","unusedVar",["26"],"no-undef","'i' is not defined.","undef",["27"],["28"],["29"],["30"],["31"],{"kind":"32","justification":"33"},{"kind":"32","justification":"33"},{"kind":"32","justification":"33"},{"kind":"32","justification":"33"},{"kind":"32","justification":"33"},{"kind":"32","justification":"33"},"directive",""]
1
+ [{"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/lib/index.js":"1","/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/tests/index.js":"2"},{"size":4171,"mtime":1644358226139,"results":"3","hashOfConfig":"4"},{"size":862,"mtime":1643928622922,"results":"5","hashOfConfig":"4"},{"filePath":"6","messages":"7","suppressedMessages":"8","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"uajcbk",{"filePath":"9","messages":"10","suppressedMessages":"11","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/lib/index.js",[],[],"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/tests/index.js",[],[]]
@@ -0,0 +1 @@
1
+ {"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/lib/index.js":{"path":"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/lib/index.js","statementMap":{"0":{"start":{"line":7,"column":17},"end":{"line":10,"column":1}},"1":{"start":{"line":18,"column":2},"end":{"line":18,"column":52}},"2":{"start":{"line":31,"column":29},"end":{"line":31,"column":31}},"3":{"start":{"line":34,"column":24},"end":{"line":34,"column":67}},"4":{"start":{"line":35,"column":24},"end":{"line":35,"column":59}},"5":{"start":{"line":37,"column":4},"end":{"line":76,"column":7}},"6":{"start":{"line":48,"column":6},"end":{"line":75,"column":7}},"7":{"start":{"line":49,"column":28},"end":{"line":49,"column":34}},"8":{"start":{"line":50,"column":26},"end":{"line":50,"column":30}},"9":{"start":{"line":51,"column":20},"end":{"line":51,"column":51}},"10":{"start":{"line":54,"column":22},"end":{"line":54,"column":62}},"11":{"start":{"line":57,"column":8},"end":{"line":74,"column":9}},"12":{"start":{"line":58,"column":10},"end":{"line":73,"column":11}},"13":{"start":{"line":58,"column":23},"end":{"line":58,"column":24}},"14":{"start":{"line":59,"column":27},"end":{"line":59,"column":65}},"15":{"start":{"line":60,"column":35},"end":{"line":60,"column":121}},"16":{"start":{"line":65,"column":38},"end":{"line":65,"column":99}},"17":{"start":{"line":67,"column":12},"end":{"line":71,"column":15}},"18":{"start":{"line":79,"column":4},"end":{"line":83,"column":7}},"19":{"start":{"line":80,"column":6},"end":{"line":82,"column":7}},"20":{"start":{"line":81,"column":8},"end":{"line":81,"column":27}},"21":{"start":{"line":85,"column":4},"end":{"line":85,"column":27}},"22":{"start":{"line":98,"column":2},"end":{"line":110,"column":5}},"23":{"start":{"line":100,"column":21},"end":{"line":100,"column":48}},"24":{"start":{"line":103,"column":4},"end":{"line":108,"column":5}},"25":{"start":{"line":104,"column":23},"end":{"line":104,"column":51}},"26":{"start":{"line":105,"column":6},"end":{"line":105,"column":115}},"27":{"start":{"line":107,"column":6},"end":{"line":107,"column":23}},"28":{"start":{"line":124,"column":2},"end":{"line":124,"column":38}},"29":{"start":{"line":126,"column":2},"end":{"line":137,"column":4}},"30":{"start":{"line":127,"column":4},"end":{"line":127,"column":23}},"31":{"start":{"line":130,"column":29},"end":{"line":130,"column":64}},"32":{"start":{"line":133,"column":4},"end":{"line":135,"column":5}},"33":{"start":{"line":134,"column":6},"end":{"line":134,"column":55}},"34":{"start":{"line":140,"column":0},"end":{"line":140,"column":38}}},"fnMap":{"0":{"name":"normalizeOptions","decl":{"start":{"line":17,"column":10},"end":{"line":17,"column":26}},"loc":{"start":{"line":17,"column":36},"end":{"line":19,"column":1}},"line":17},"1":{"name":"getMarkdownIncludes","decl":{"start":{"line":30,"column":9},"end":{"line":30,"column":28}},"loc":{"start":{"line":30,"column":45},"end":{"line":86,"column":1}},"line":30},"2":{"name":"(anonymous_2)","decl":{"start":{"line":37,"column":31},"end":{"line":37,"column":32}},"loc":{"start":{"line":37,"column":47},"end":{"line":76,"column":5}},"line":37},"3":{"name":"(anonymous_3)","decl":{"start":{"line":79,"column":31},"end":{"line":79,"column":32}},"loc":{"start":{"line":79,"column":47},"end":{"line":83,"column":5}},"line":79},"4":{"name":"resolveMarkdownIncludes","decl":{"start":{"line":96,"column":9},"end":{"line":96,"column":32}},"loc":{"start":{"line":96,"column":58},"end":{"line":111,"column":1}},"line":96},"5":{"name":"(anonymous_5)","decl":{"start":{"line":98,"column":27},"end":{"line":98,"column":28}},"loc":{"start":{"line":98,"column":53},"end":{"line":110,"column":3}},"line":98},"6":{"name":"initMarkdownPartials","decl":{"start":{"line":123,"column":9},"end":{"line":123,"column":29}},"loc":{"start":{"line":123,"column":39},"end":{"line":138,"column":1}},"line":123},"7":{"name":"markdownPartials","decl":{"start":{"line":126,"column":18},"end":{"line":126,"column":34}},"loc":{"start":{"line":126,"column":60},"end":{"line":137,"column":3}},"line":126}},"branchMap":{"0":{"loc":{"start":{"line":18,"column":37},"end":{"line":18,"column":50}},"type":"binary-expr","locations":[{"start":{"line":18,"column":37},"end":{"line":18,"column":44}},{"start":{"line":18,"column":48},"end":{"line":18,"column":50}}],"line":18},"1":{"loc":{"start":{"line":48,"column":6},"end":{"line":75,"column":7}},"type":"if","locations":[{"start":{"line":48,"column":6},"end":{"line":75,"column":7}},{"start":{"line":48,"column":6},"end":{"line":75,"column":7}}],"line":48},"2":{"loc":{"start":{"line":48,"column":10},"end":{"line":48,"column":76}},"type":"binary-expr","locations":[{"start":{"line":48,"column":10},"end":{"line":48,"column":43}},{"start":{"line":48,"column":47},"end":{"line":48,"column":76}}],"line":48},"3":{"loc":{"start":{"line":57,"column":8},"end":{"line":74,"column":9}},"type":"if","locations":[{"start":{"line":57,"column":8},"end":{"line":74,"column":9}},{"start":{"line":57,"column":8},"end":{"line":74,"column":9}}],"line":57},"4":{"loc":{"start":{"line":80,"column":6},"end":{"line":82,"column":7}},"type":"if","locations":[{"start":{"line":80,"column":6},"end":{"line":82,"column":7}},{"start":{"line":80,"column":6},"end":{"line":82,"column":7}}],"line":80},"5":{"loc":{"start":{"line":133,"column":4},"end":{"line":135,"column":5}},"type":"if","locations":[{"start":{"line":133,"column":4},"end":{"line":135,"column":5}},{"start":{"line":133,"column":4},"end":{"line":135,"column":5}}],"line":133}},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":1,"6":2,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1,"13":1,"14":1,"15":1,"16":1,"17":1,"18":1,"19":2,"20":1,"21":1,"22":1,"23":1,"24":1,"25":1,"26":1,"27":0,"28":1,"29":1,"30":1,"31":1,"32":1,"33":1,"34":1},"f":{"0":1,"1":1,"2":2,"3":2,"4":1,"5":1,"6":1,"7":1},"b":{"0":[1,0],"1":[1,1],"2":[2,2],"3":[1,0],"4":[1,1],"5":[1,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"5a1610d35933a6d0de98c2efdfcab0bbaafbd6bd","contentHash":"0267ccac04a4935df40548e98e2dbbb2d1c5b36280a2a47d872df36ae3e24b14"}}
@@ -0,0 +1 @@
1
+ {"parent":null,"pid":61877,"argv":["/Users/wernerglinka/.nvm/versions/node/v16.13.0/bin/node","/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/node_modules/.bin/mocha","./tests/index.js"],"execArgv":[],"cwd":"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials","time":1644605272274,"ppid":61876,"coverageFilename":"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/.nyc_output/a0da4073-c568-4956-847a-81fd95f3459d.json","externalId":"","uuid":"a0da4073-c568-4956-847a-81fd95f3459d","files":["/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/lib/index.js"]}
@@ -1 +1 @@
1
- {"processes":{"ffd8939f-f238-4153-bb5c-abd8ff56501c":{"parent":null,"children":[]}},"files":{},"externalIds":{}}
1
+ {"processes":{"a0da4073-c568-4956-847a-81fd95f3459d":{"parent":null,"children":[]}},"files":{"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/lib/index.js":["a0da4073-c568-4956-847a-81fd95f3459d"]},"externalIds":{}}
package/README.md CHANGED
@@ -1,26 +1,12 @@
1
1
  # metalsmith-markdown-partials
2
2
 
3
- This Metalsmith plugin enables the use of Markdown partials, e.g. Markdown fragments can be inserted into the contents section of a page markdown file via an include marker.
3
+ A Metalsmith plugin that enables the use of Markdown partials.
4
4
 
5
- ```
6
- {#md "<file name>.md" #}
7
- ```
8
-
9
- This allows for modular markdown and promotes reuse of markdown partials.
10
-
11
- ## Page Markdown File
12
-
13
- A markdown file that will be transformed into an html file via a template
14
-
15
- ## Markdown Partials
16
-
17
- A markdown file to be inserted into a Page Markdown file
5
+ [![metalsmith: plugin][metalsmith-badge]][metalsmith-url]
6
+ [![npm: version][npm-badge]][npm-url]
7
+ [![license: ISC][license-badge]][license-url]
18
8
 
19
- Markdown partials are located in a separate directory, for example /md-library/. This directory should be located outside _/src/content/_ .
20
-
21
- Partial markdown files have the extention **.md**. A markdown partial file does NOT have frontmatter metadata, only the markdown to be inserted into a page markdown file.
22
-
23
- The markdown partials directory's default location is './src/md-library/'. The partials directory can be set via the libraryPath option.
9
+ Markdown fragments are be 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.
24
10
 
25
11
  ## Installation
26
12
 
@@ -35,7 +21,7 @@ var mdPartials = require('metalsmith-markdown-partials');
35
21
 
36
22
  ...
37
23
  .use(mdPartials({
38
- libraryPath: './src/md-partials/',
24
+ libraryPath: './markdown-partials/',
39
25
  fileSuffix: '.md.njk',
40
26
  }))
41
27
  ...
@@ -46,20 +32,20 @@ var mdPartials = require('metalsmith-markdown-partials');
46
32
 
47
33
  **libraryPath**
48
34
 
49
- The default libraryPath is './src/md-partials/'. This default assumes that all pages are located in './src/content/\*' so the markdown partials are located outside the metalsmith source directory.
35
+ The default libraryPath is `./markdown-partials/`. This default assumes that all pages are located in `./src/` so the markdown partials are located inside the metalsmith source directory.
50
36
 
51
37
  ```js
52
38
  const mdPartials = require('metalsmith-markdown-partials');
53
39
 
54
40
  metalsmith(__dirname)
55
41
  .use(mdPartials({
56
- libraryPath: './src/md-partials/'
42
+ libraryPath: './markdown-partials/'
57
43
  })
58
44
  ```
59
45
 
60
46
  **fileSuffix**
61
47
 
62
- The default fileSuffix is 'md', but depending on which templating language is used, the suffix will have two parts. For example for Nunjucks it will be 'md.njk'.
48
+ The default fileSuffix is `.md`, but depending on which templating language is used, the suffix will have two parts. For example for Nunjucks it will be `md.njk`.
63
49
 
64
50
  ```js
65
51
  const mdPartials = require('metalsmith-markdown-partials').use(
@@ -71,7 +57,27 @@ const mdPartials = require('metalsmith-markdown-partials').use(
71
57
 
72
58
  ## How it works
73
59
 
74
- ### index.md
60
+ ### Replacement marker
61
+ ```
62
+ {#md "<file name>.md" #}
63
+ ```
64
+
65
+ ### Page Markdown File
66
+ A markdown file that will be transformed into an html file via a template
67
+
68
+ ### Markdown Partial
69
+
70
+ A markdown file to be inserted into a Page Markdown file
71
+
72
+ Markdown partials are located in a separate directory, for example `/markdown-library/`. This directory should be located inside `/src/` .
73
+
74
+ Partial markdown files have the extention `.md`. A markdown partial file does NOT have frontmatter metadata, only the markdown to be inserted into a page markdown file.
75
+
76
+ The markdown partials directory's default location is `./src/markdown-library/`. The partials directory can be set via the libraryPath option.
77
+
78
+ ## Example
79
+
80
+ **index.md**
75
81
 
76
82
  ```markdown
77
83
  # This is an Example Page
@@ -83,7 +89,7 @@ Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus
83
89
  Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
84
90
  ```
85
91
 
86
- ### example_partial.md
92
+ **example_partial.md**
87
93
 
88
94
  ```markdown
89
95
  ## Inserted Content
@@ -91,7 +97,7 @@ Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac fa
91
97
  Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Nulla vitae elit libero, a pharetra augue.
92
98
  ```
93
99
 
94
- ### index.html
100
+ **index.html**
95
101
 
96
102
  ```html
97
103
  <h1>This is an Example Page</h1>
@@ -111,11 +117,50 @@ Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Nulla vitae
111
117
  parturient montes, nascetur ridiculus mus.
112
118
  </p>
113
119
  ```
120
+ ## Debug
121
+
122
+ To enable debug logs, set the `DEBUG` environment variable to `metalsmith-markdown-partials`:
123
+
124
+ Linux/Mac:
125
+
126
+ ```
127
+ DEBUG=metalsmith-markdown-partials
128
+ ```
114
129
 
115
- ## Authors
130
+ Windows:
131
+
132
+ ```
133
+ set "DEBUG=metalsmith-markdown-partials"
134
+ ```
135
+ ### CLI usage
136
+
137
+ To use this plugin with the Metalsmith CLI, add `metalsmith-markdown-partials` to the `plugins` key in your `metalsmith.json` file:
138
+
139
+ ```json
140
+ {
141
+ "plugins": [
142
+ {
143
+ "metalsmith-markdown-partials": {
144
+ "libraryPath": "./markdown-partials/",
145
+ "fileSuffix": ".md.njk"
146
+ }
147
+ }
148
+ ]
149
+ }
150
+ ```
151
+
152
+ ## Author
116
153
 
117
154
  - [werner@glinka.co](https://github.com/wernerglinka)
118
155
 
119
156
  ## License
120
157
 
121
158
  Code released under [the ISC license](https://github.com/wernerglinka/metalsmith-markdown-partials/blob/main/LICENSE).
159
+
160
+
161
+ [npm-badge]: https://img.shields.io/npm/v/metalsmith-markdown-partials.svg
162
+ [npm-url]: https://www.npmjs.com/package/metalsmith-markdown-partials
163
+ [metalsmith-badge]: https://img.shields.io/badge/metalsmith-plugin-green.svg?longCache=true
164
+ [metalsmith-url]: https://metalsmith.io
165
+ [license-badge]: https://img.shields.io/github/license/wernerglinka/metalsmith-markdown-partials
166
+ [license-url]: LICENSE
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
+ const 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.4",
4
4
  "description": "A Metalsmith plugin that allows the use of partial markdowm files",
5
5
  "keywords": [
6
6
  "metalsmith-plugin",
@@ -27,24 +27,26 @@
27
27
  "debug": "^4.3.3"
28
28
  },
29
29
  "devDependencies": {
30
- "auto-changelog": "^2.3.0",
30
+ "auto-changelog": "^2.4.0",
31
31
  "chai": "^4.3.6",
32
32
  "coveralls": "^3.1.1",
33
33
  "eslint": "^8.4.1",
34
34
  "eslint-config-prettier": "^8.3.0",
35
35
  "metalsmith": "^2.4.1",
36
36
  "mocha": "^9.2.0",
37
- "prettier": "^2.5.1"
37
+ "nyc": "^15.1.0",
38
+ "prettier": "^2.5.1",
39
+ "release-it": "^14.12.4"
38
40
  },
39
41
  "peerDependencies": {
40
- "metalsmith": "^2.3.0"
42
+ "metalsmith": "^2.4.1"
41
43
  },
42
44
  "scripts": {
43
45
  "changelog": "auto-changelog -u date --commit-limit false --ignore-commit-pattern '^((dev|chore|ci):|Release)'",
44
46
  "format": "prettier --write \"**/*.{yml,md,js,json}\"",
45
47
  "lint": "eslint --cache --fix-dry-run .",
46
48
  "release": "release-it .",
47
- "test": "mocha ./tests/index.js"
49
+ "test": "nyc mocha ./tests/index.js"
48
50
  },
49
51
  "engines": {
50
52
  "node": ">=8"
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 => {
@@ -1 +0,0 @@
1
- {"parent":null,"pid":29402,"argv":["/Users/wernerglinka/.nvm/versions/node/v16.13.0/bin/node","/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/node_modules/.bin/mocha"],"execArgv":[],"cwd":"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials","time":1643757001172,"ppid":29401,"coverageFilename":"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/.nyc_output/ffd8939f-f238-4153-bb5c-abd8ff56501c.json","externalId":"","uuid":"ffd8939f-f238-4153-bb5c-abd8ff56501c","files":[]}