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 +1 -1
- package/.nyc_output/a0da4073-c568-4956-847a-81fd95f3459d.json +1 -0
- package/.nyc_output/processinfo/a0da4073-c568-4956-847a-81fd95f3459d.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -1
- package/README.md +72 -27
- package/lib/index.js +85 -94
- package/metalsmith-markdown-partials-2.0.3.tgz +0 -0
- package/package.json +7 -5
- package/tests/fixtures/{md-partials → src/md-partials}/test-partial.md +0 -0
- package/tests/index.js +1 -1
- package/.nyc_output/ffd8939f-f238-4153-bb5c-abd8ff56501c.json +0 -1
- package/.nyc_output/processinfo/ffd8939f-f238-4153-bb5c-abd8ff56501c.json +0 -1
package/.eslintcache
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
[{"/Users/wernerglinka/Documents/Projects/1-SANDBOXES/metalsmith-sandbox/>>>plugins/ms-markdown-partials/lib/index.js":"1"
|
|
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":{"
|
|
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
|
-
|
|
3
|
+
A Metalsmith plugin that enables the use of Markdown partials.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
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
|
|
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: './
|
|
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
|
|
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: './
|
|
42
|
+
libraryPath: './markdown-partials/'
|
|
57
43
|
})
|
|
58
44
|
```
|
|
59
45
|
|
|
60
46
|
**fileSuffix**
|
|
61
47
|
|
|
62
|
-
The default fileSuffix is
|
|
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
|
-
###
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
+
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
|
-
*
|
|
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;
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metalsmith-markdown-partials",
|
|
3
|
-
"version": "2.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.
|
|
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
|
-
"
|
|
37
|
+
"nyc": "^15.1.0",
|
|
38
|
+
"prettier": "^2.5.1",
|
|
39
|
+
"release-it": "^14.12.4"
|
|
38
40
|
},
|
|
39
41
|
"peerDependencies": {
|
|
40
|
-
"metalsmith": "^2.
|
|
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"
|
|
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 => {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{}
|
|
@@ -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":[]}
|