@salesforcedevs/sfdocs-remark-include-plugin 0.0.5-alpha → 0.0.6-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 +3 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -18
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/index.test.ts +6 -3
- package/src/index.ts +5 -19
package/README.md
CHANGED
|
@@ -1,21 +1,13 @@
|
|
|
1
1
|
# Remark Include Plugin
|
|
2
2
|
|
|
3
|
-
## Usage
|
|
4
|
-
|
|
5
|
-
Use with `remark-directive` in your unified pipeline:
|
|
6
|
-
|
|
7
3
|
## Examples
|
|
8
4
|
|
|
9
|
-
|
|
5
|
+
Include another markdown file:
|
|
10
6
|
|
|
11
7
|
```
|
|
12
8
|
::include{src="../shared/snippet.md"}
|
|
13
9
|
```
|
|
14
10
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
::include{src="../shared/test_include.md"}
|
|
19
|
-
```
|
|
11
|
+
The plugin reads the target file, parses it, and inserts its children into the current document.
|
|
20
12
|
|
|
21
|
-
If the included file path contains `/shared/`, relative
|
|
13
|
+
If the included file path contains `/shared/`, relative image URLs in that included content are rewritten to absolute content paths.
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAoCtC;;;;;;;;GAQG;AACH,iBAAS,aAAa,UACmB,WAAW,CAuBnD;AAED,eAAe,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const unist_util_visit_1 = __importDefault(require("unist-util-visit"));
|
|
7
|
-
const hastscript_1 = __importDefault(require("hastscript"));
|
|
8
7
|
const path_1 = __importDefault(require("path"));
|
|
9
8
|
const fs_1 = __importDefault(require("fs"));
|
|
10
9
|
const vfile_1 = __importDefault(require("vfile"));
|
|
@@ -12,6 +11,7 @@ const INCLUDE_DIRECTIVE = 'include';
|
|
|
12
11
|
const LEAF_DIRECTIVE_TYPE = 'leafDirective';
|
|
13
12
|
const CONTENT_FOLDER = '/content/';
|
|
14
13
|
const SHARED_PATH_SEGMENT = '/shared/';
|
|
14
|
+
const MEDIA_FOLDER = '/media';
|
|
15
15
|
function getContentDirPath(curFilePath) {
|
|
16
16
|
const position = curFilePath.indexOf(CONTENT_FOLDER);
|
|
17
17
|
if (position !== -1) {
|
|
@@ -20,13 +20,8 @@ function getContentDirPath(curFilePath) {
|
|
|
20
20
|
return '';
|
|
21
21
|
}
|
|
22
22
|
function getSubstringAfterMedia(folderPath) {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
return folderPath.substring(idx + 1);
|
|
26
|
-
const idx2 = folderPath.indexOf('media');
|
|
27
|
-
if (idx2 !== -1)
|
|
28
|
-
return folderPath.substring(idx2);
|
|
29
|
-
return folderPath;
|
|
23
|
+
const index = folderPath.indexOf(MEDIA_FOLDER);
|
|
24
|
+
return folderPath.substring(index);
|
|
30
25
|
}
|
|
31
26
|
function transformImageLinksInTree(root, contentDir) {
|
|
32
27
|
if (!contentDir)
|
|
@@ -59,10 +54,6 @@ function includePlugin() {
|
|
|
59
54
|
return;
|
|
60
55
|
const src = (_a = node.attributes) === null || _a === void 0 ? void 0 : _a.src;
|
|
61
56
|
const filename = path_1.default.resolve(file.dirname || '', src);
|
|
62
|
-
if (!fs_1.default.existsSync(filename)) {
|
|
63
|
-
file.fail(`File not found ${filename}`, node);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
57
|
try {
|
|
67
58
|
const content = fs_1.default.readFileSync(filename);
|
|
68
59
|
const includeFile = (0, vfile_1.default)({ path: filename, contents: content });
|
|
@@ -71,12 +62,7 @@ function includePlugin() {
|
|
|
71
62
|
transformImageLinksInTree(root, getContentDirPath(filename));
|
|
72
63
|
}
|
|
73
64
|
const { children } = root;
|
|
74
|
-
|
|
75
|
-
const hast = (0, hastscript_1.default)(INCLUDE_DIRECTIVE, node.attributes || {});
|
|
76
|
-
// node.type = 'containerDirective';
|
|
77
|
-
node.children = Array.isArray(children) ? children : [];
|
|
78
|
-
data.hName = INCLUDE_DIRECTIVE;
|
|
79
|
-
data.hProperties = hast.properties;
|
|
65
|
+
parent.children.splice(i + 1, 0, ...(Array.isArray(children) ? children : []));
|
|
80
66
|
}
|
|
81
67
|
catch (err) {
|
|
82
68
|
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;
|
|
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,C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,OAAO,CAAC,IAAI,CAAC,OAAO,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC;gBAEvD,IAAI;oBACA,MAAM,OAAO,GAAG,YAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;oBAC1C,MAAM,WAAW,GAAG,IAAA,eAAK,EAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;oBACjE,IAAI,IAAI,GAAG,SAAS,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;oBACxC,IAAI,QAAQ,CAAC,QAAQ,CAAC,mBAAmB,CAAC,EAAE;wBACxC,yBAAyB,CAAC,IAAI,EAAE,iBAAiB,CAAC,QAAQ,CAAC,CAAC,CAAC;qBAChE;oBACD,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;oBAC1B,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;iBAClF;gBAAC,OAAO,GAAQ,EAAE;oBACf,IAAI,CAAC,IAAI,CAAC,CAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,OAAO,KAAI,uBAAuB,QAAQ,EAAE,EAAE,IAAI,CAAC,CAAC;iBACtE;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC;IACN,CAAC,CAAC;AACN,CAAC;AAED,kBAAe,aAAa,CAAC"}
|
package/package.json
CHANGED
|
@@ -39,7 +39,7 @@ describe('Include plugin test', () => {
|
|
|
39
39
|
jest.restoreAllMocks();
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
test('
|
|
42
|
+
test('includes markdown content from a shared file', async () => {
|
|
43
43
|
fs.mkdirSync(path.join(testRoot, 'abc/xyz/guides'), { recursive: true });
|
|
44
44
|
fs.mkdirSync(path.join(testRoot, 'abc/xyz/shared'), { recursive: true });
|
|
45
45
|
fs.writeFileSync(
|
|
@@ -54,11 +54,12 @@ describe('Include plugin test', () => {
|
|
|
54
54
|
`);
|
|
55
55
|
const file = vfile({ path: path.join(testRoot, 'abc/xyz/guides/parent.md'), contents: content });
|
|
56
56
|
const transformedHtml = await processMarkdown(file);
|
|
57
|
+
|
|
57
58
|
expect(transformedHtml.contents).toContain('<h2>Include file</h2>');
|
|
58
59
|
expect(transformedHtml.contents).toContain('<p>shared content</p>');
|
|
59
60
|
});
|
|
60
61
|
|
|
61
|
-
test('
|
|
62
|
+
test('rewrites shared include media image URL to content media path', async () => {
|
|
62
63
|
fs.mkdirSync(path.join(testRoot, 'abc/xyz/content/guides'), { recursive: true });
|
|
63
64
|
fs.mkdirSync(path.join(testRoot, 'abc/xyz/content/shared'), { recursive: true });
|
|
64
65
|
fs.writeFileSync(
|
|
@@ -77,11 +78,12 @@ describe('Include plugin test', () => {
|
|
|
77
78
|
contents: content,
|
|
78
79
|
});
|
|
79
80
|
const transformedHtml = await processMarkdown(file);
|
|
81
|
+
|
|
80
82
|
expect(transformedHtml.contents).toContain('<h2>Include file</h2>');
|
|
81
83
|
expect(transformedHtml.contents).toContain(path.join(testRoot, 'abc/xyz/content/media/test.jpeg'));
|
|
82
84
|
});
|
|
83
85
|
|
|
84
|
-
test('
|
|
86
|
+
test('rewrites localized shared include media image URL to root content media path', async () => {
|
|
85
87
|
fs.mkdirSync(path.join(testRoot, 'abc/content/ja-jp/guides'), { recursive: true });
|
|
86
88
|
fs.mkdirSync(path.join(testRoot, 'abc/content/ja-jp/shared'), { recursive: true });
|
|
87
89
|
fs.writeFileSync(
|
|
@@ -100,6 +102,7 @@ describe('Include plugin test', () => {
|
|
|
100
102
|
contents: content,
|
|
101
103
|
});
|
|
102
104
|
const transformedHtml = await processMarkdown(file);
|
|
105
|
+
|
|
103
106
|
expect(transformedHtml.contents).toContain(path.join(testRoot, 'abc/content/media/test.jpeg'));
|
|
104
107
|
});
|
|
105
108
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import visit from 'unist-util-visit';
|
|
2
|
-
import h from 'hastscript';
|
|
3
2
|
import { Transformer } from 'unified';
|
|
4
3
|
import path from 'path';
|
|
5
4
|
import fs from 'fs';
|
|
@@ -9,6 +8,7 @@ const INCLUDE_DIRECTIVE = 'include';
|
|
|
9
8
|
const LEAF_DIRECTIVE_TYPE = 'leafDirective';
|
|
10
9
|
const CONTENT_FOLDER = '/content/';
|
|
11
10
|
const SHARED_PATH_SEGMENT = '/shared/';
|
|
11
|
+
const MEDIA_FOLDER = '/media';
|
|
12
12
|
|
|
13
13
|
function getContentDirPath(curFilePath: string): string {
|
|
14
14
|
const position = curFilePath.indexOf(CONTENT_FOLDER);
|
|
@@ -18,14 +18,9 @@ function getContentDirPath(curFilePath: string): string {
|
|
|
18
18
|
return '';
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
function getSubstringAfterMedia(folderPath: string)
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
if (idx !== -1) return folderPath.substring(idx + 1);
|
|
25
|
-
const idx2 = folderPath.indexOf('media');
|
|
26
|
-
|
|
27
|
-
if (idx2 !== -1) return folderPath.substring(idx2);
|
|
28
|
-
return folderPath;
|
|
21
|
+
function getSubstringAfterMedia(folderPath: string) {
|
|
22
|
+
const index = folderPath.indexOf(MEDIA_FOLDER);
|
|
23
|
+
return folderPath.substring(index);
|
|
29
24
|
}
|
|
30
25
|
|
|
31
26
|
function transformImageLinksInTree(root: any, contentDir: string): void {
|
|
@@ -57,10 +52,6 @@ function includePlugin() {
|
|
|
57
52
|
if (node.type !== LEAF_DIRECTIVE_TYPE || node.name !== INCLUDE_DIRECTIVE || !parent) return;
|
|
58
53
|
const src = node.attributes?.src;
|
|
59
54
|
const filename = path.resolve(file.dirname || '', src);
|
|
60
|
-
if (!fs.existsSync(filename)) {
|
|
61
|
-
file.fail(`File not found ${filename}`, node);
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
55
|
|
|
65
56
|
try {
|
|
66
57
|
const content = fs.readFileSync(filename);
|
|
@@ -70,12 +61,7 @@ function includePlugin() {
|
|
|
70
61
|
transformImageLinksInTree(root, getContentDirPath(filename));
|
|
71
62
|
}
|
|
72
63
|
const { children } = root;
|
|
73
|
-
|
|
74
|
-
const hast = h(INCLUDE_DIRECTIVE, node.attributes || {});
|
|
75
|
-
// node.type = 'containerDirective';
|
|
76
|
-
node.children = Array.isArray(children) ? children : [];
|
|
77
|
-
data.hName = INCLUDE_DIRECTIVE;
|
|
78
|
-
data.hProperties = hast.properties;
|
|
64
|
+
parent.children.splice(i + 1, 0, ...(Array.isArray(children) ? children : []));
|
|
79
65
|
} catch (err: any) {
|
|
80
66
|
file.fail(err?.message || `Failed to read file ${filename}`, node);
|
|
81
67
|
}
|