@salesforcedevs/sfdocs-remark-include-plugin 0.0.6-alpha → 0.2.0-alpha
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 +2 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -6
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/index.test.ts +29 -0
- package/src/index.ts +17 -6
package/README.md
CHANGED
|
@@ -11,3 +11,5 @@ Include another markdown file:
|
|
|
11
11
|
The plugin reads the target file, parses it, and inserts its children into the current document.
|
|
12
12
|
|
|
13
13
|
If the included file path contains `/shared/`, relative image URLs in that included content are rewritten to absolute content paths.
|
|
14
|
+
|
|
15
|
+
You can pass custom render functions as the plugin’s second argument, e.g. for a VS Code extension.
|
package/dist/index.d.ts
CHANGED
|
@@ -6,8 +6,9 @@ import { Transformer } from 'unified';
|
|
|
6
6
|
* with the included content. Must be used after remark-directive.
|
|
7
7
|
* When the included file path contains /shared/, image URLs in that content are rewritten to absolute paths using the content directory.
|
|
8
8
|
*
|
|
9
|
+
* @param customRenderFunctions - Optional; for custom rendering functions (e.g. `renderInclude`).
|
|
9
10
|
* @returns Function that returns a Remark transformer
|
|
10
11
|
*/
|
|
11
|
-
declare function includePlugin(): () => Transformer;
|
|
12
|
+
declare function includePlugin(customRenderFunctions?: any): () => Transformer;
|
|
12
13
|
export default includePlugin;
|
|
13
14
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAoCtC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAoCtC;;;;;;;;;GASG;AACH,iBAAS,aAAa,CAAC,qBAAqB,CAAC,KAAA,SACJ,WAAW,CAiCnD;AAED,eAAe,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -42,9 +42,10 @@ function transformImageLinksInTree(root, contentDir) {
|
|
|
42
42
|
* with the included content. Must be used after remark-directive.
|
|
43
43
|
* When the included file path contains /shared/, image URLs in that content are rewritten to absolute paths using the content directory.
|
|
44
44
|
*
|
|
45
|
+
* @param customRenderFunctions - Optional; for custom rendering functions (e.g. `renderInclude`).
|
|
45
46
|
* @returns Function that returns a Remark transformer
|
|
46
47
|
*/
|
|
47
|
-
function includePlugin() {
|
|
48
|
+
function includePlugin(customRenderFunctions) {
|
|
48
49
|
return function includePluginImpl() {
|
|
49
50
|
const processor = this;
|
|
50
51
|
return function transformer(tree, file) {
|
|
@@ -57,12 +58,23 @@ function includePlugin() {
|
|
|
57
58
|
try {
|
|
58
59
|
const content = fs_1.default.readFileSync(filename);
|
|
59
60
|
const includeFile = (0, vfile_1.default)({ path: filename, contents: content });
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
if (customRenderFunctions === null || customRenderFunctions === void 0 ? void 0 : customRenderFunctions.renderInclude) {
|
|
62
|
+
const root = processor.processSync(includeFile);
|
|
63
|
+
const metadata = new Map([
|
|
64
|
+
['root', root],
|
|
65
|
+
['node', node],
|
|
66
|
+
]);
|
|
67
|
+
const out = customRenderFunctions.renderInclude(metadata).get('outputDom');
|
|
68
|
+
parent.children.splice(i, 1, { type: 'html', value: out !== null && out !== void 0 ? out : '' });
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
let root = processor.parse(includeFile);
|
|
72
|
+
if (filename.includes(SHARED_PATH_SEGMENT)) {
|
|
73
|
+
transformImageLinksInTree(root, getContentDirPath(filename));
|
|
74
|
+
}
|
|
75
|
+
const { children } = root;
|
|
76
|
+
parent.children.splice(i + 1, 0, ...(Array.isArray(children) ? children : []));
|
|
63
77
|
}
|
|
64
|
-
const { children } = root;
|
|
65
|
-
parent.children.splice(i + 1, 0, ...(Array.isArray(children) ? children : []));
|
|
66
78
|
}
|
|
67
79
|
catch (err) {
|
|
68
80
|
file.fail((err === null || err === void 0 ? void 0 : err.message) || `Failed to read file ${filename}`, node);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,wEAAqC;AAErC,gDAAwB;AACxB,4CAAoB;AACpB,kDAAqC;AAErC,MAAM,iBAAiB,GAAG,SAAS,CAAC;AACpC,MAAM,mBAAmB,GAAG,eAAe,CAAC;AAC5C,MAAM,cAAc,GAAG,WAAW,CAAC;AACnC,MAAM,mBAAmB,GAAG,UAAU,CAAC;AACvC,MAAM,YAAY,GAAG,QAAQ,CAAC;AAE9B,SAAS,iBAAiB,CAAC,WAAmB;IAC1C,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;IACrD,IAAI,QAAQ,KAAK,CAAC,CAAC,EAAE;QACjB,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;KACjE;IACD,OAAO,EAAE,CAAC;AACd,CAAC;AAED,SAAS,sBAAsB,CAAC,UAAkB;IAC9C,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAC/C,OAAO,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AACvC,CAAC;AAED,SAAS,yBAAyB,CAAC,IAAS,EAAE,UAAkB;IAC5D,IAAI,CAAC,UAAU;QAAE,OAAO;IACxB,IAAA,0BAAK,EAAC,IAAI,EAAE,OAAO,EAAE,CAAC,IAAS,EAAE,EAAE;QAC/B,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC;QAC1B,MAAM,aAAa,GAAG,CAAC,C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{
|
|
2
|
-
"version": "0.0
|
|
2
|
+
"version": "0.2.0-alpha",
|
|
3
3
|
"name": "@salesforcedevs/sfdocs-remark-include-plugin",
|
|
4
4
|
"description": "SFDocs remark plugin for the include directive (::include)",
|
|
5
5
|
"author": "SFDocs Team",
|
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
"types": "dist/index.d.ts",
|
|
21
21
|
"prettier": "@salesforcedevs/sfdocs-prettier",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@salesforcedevs/sfdocs-prettier": "latest",
|
|
24
23
|
"rehype-raw": "^5.0.0",
|
|
25
24
|
"rehype-stringify": "^8.0.0",
|
|
26
25
|
"remark-directive": "^1.0.1",
|
|
@@ -33,6 +32,7 @@
|
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
34
|
"@babel/preset-typescript": "7.10.4",
|
|
35
|
+
"@salesforcedevs/sfdocs-prettier": "latest",
|
|
36
36
|
"@types/jest": "^26.0.23",
|
|
37
37
|
"@types/node": "^15.6.1",
|
|
38
38
|
"jest": "^29.7.0",
|
|
@@ -105,4 +105,33 @@ describe('Include plugin test', () => {
|
|
|
105
105
|
|
|
106
106
|
expect(transformedHtml.contents).toContain(path.join(testRoot, 'abc/content/media/test.jpeg'));
|
|
107
107
|
});
|
|
108
|
+
|
|
109
|
+
test('renderInclude replaces directive with HTML from outputDom', async () => {
|
|
110
|
+
fs.mkdirSync(path.join(testRoot, 'guides'), { recursive: true });
|
|
111
|
+
fs.writeFileSync(path.join(testRoot, 'guides/included.md'), '# Included heading');
|
|
112
|
+
const content = stripIndent(`
|
|
113
|
+
::include{src="included.md"}
|
|
114
|
+
`);
|
|
115
|
+
const file = vfile({ path: path.join(testRoot, 'guides/parent.md'), contents: content });
|
|
116
|
+
const transformedHtml = unified()
|
|
117
|
+
.use(parse)
|
|
118
|
+
.use(directive)
|
|
119
|
+
.use(
|
|
120
|
+
includePlugin({
|
|
121
|
+
renderInclude: () => {
|
|
122
|
+
const m = new Map<string, string>();
|
|
123
|
+
m.set('outputDom', '<section class="include-custom">custom block</section>');
|
|
124
|
+
return m;
|
|
125
|
+
},
|
|
126
|
+
}),
|
|
127
|
+
)
|
|
128
|
+
.use(remark2rehype, { allowDangerousHtml: true })
|
|
129
|
+
.use(rawHtml)
|
|
130
|
+
.use(remarkGfm)
|
|
131
|
+
.use(html)
|
|
132
|
+
.processSync(file);
|
|
133
|
+
|
|
134
|
+
expect(transformedHtml.contents).toContain('<section class="include-custom">custom block</section>');
|
|
135
|
+
expect(transformedHtml.contents).not.toContain('Included heading');
|
|
136
|
+
});
|
|
108
137
|
});
|
package/src/index.ts
CHANGED
|
@@ -42,9 +42,10 @@ function transformImageLinksInTree(root: any, contentDir: string): void {
|
|
|
42
42
|
* with the included content. Must be used after remark-directive.
|
|
43
43
|
* When the included file path contains /shared/, image URLs in that content are rewritten to absolute paths using the content directory.
|
|
44
44
|
*
|
|
45
|
+
* @param customRenderFunctions - Optional; for custom rendering functions (e.g. `renderInclude`).
|
|
45
46
|
* @returns Function that returns a Remark transformer
|
|
46
47
|
*/
|
|
47
|
-
function includePlugin() {
|
|
48
|
+
function includePlugin(customRenderFunctions?) {
|
|
48
49
|
return function includePluginImpl(): Transformer {
|
|
49
50
|
const processor = this;
|
|
50
51
|
return function transformer(tree: any, file: VFile): void {
|
|
@@ -56,12 +57,22 @@ function includePlugin() {
|
|
|
56
57
|
try {
|
|
57
58
|
const content = fs.readFileSync(filename);
|
|
58
59
|
const includeFile = vfile({ path: filename, contents: content });
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
if (customRenderFunctions?.renderInclude) {
|
|
61
|
+
const root = processor.processSync(includeFile);
|
|
62
|
+
const metadata = new Map([
|
|
63
|
+
['root', root],
|
|
64
|
+
['node', node],
|
|
65
|
+
]);
|
|
66
|
+
const out = customRenderFunctions.renderInclude(metadata).get('outputDom');
|
|
67
|
+
parent.children.splice(i, 1, { type: 'html', value: out ?? '' });
|
|
68
|
+
} else {
|
|
69
|
+
let root = processor.parse(includeFile);
|
|
70
|
+
if (filename.includes(SHARED_PATH_SEGMENT)) {
|
|
71
|
+
transformImageLinksInTree(root, getContentDirPath(filename));
|
|
72
|
+
}
|
|
73
|
+
const { children } = root;
|
|
74
|
+
parent.children.splice(i + 1, 0, ...(Array.isArray(children) ? children : []));
|
|
62
75
|
}
|
|
63
|
-
const { children } = root;
|
|
64
|
-
parent.children.splice(i + 1, 0, ...(Array.isArray(children) ? children : []));
|
|
65
76
|
} catch (err: any) {
|
|
66
77
|
file.fail(err?.message || `Failed to read file ${filename}`, node);
|
|
67
78
|
}
|