@docusaurus/remark-plugin-npm2yarn 2.0.0-beta.15d451942 → 2.0.0-beta.18

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.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { Plugin } from 'unified';
8
+ interface PluginOptions {
9
+ sync?: boolean;
10
+ }
11
+ declare const plugin: Plugin<[PluginOptions?]>;
12
+ export = plugin;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;AAKjD,UAAU,aAAa;IACrB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AA8CD,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CAAC,CA6BpC,CAAC;AAIF,SAAS,MAAM,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ const tslib_1 = require("tslib");
9
+ const unist_util_visit_1 = tslib_1.__importDefault(require("unist-util-visit"));
10
+ const npm_to_yarn_1 = tslib_1.__importDefault(require("npm-to-yarn"));
11
+ // E.g. global install: 'npm i' -> 'yarn'
12
+ const convertNpmToYarn = (npmCode) => (0, npm_to_yarn_1.default)(npmCode, 'yarn');
13
+ const transformNode = (node, isSync) => {
14
+ const groupIdProp = isSync ? ' groupId="npm2yarn"' : '';
15
+ const npmCode = node.value;
16
+ const yarnCode = convertNpmToYarn(node.value);
17
+ return [
18
+ {
19
+ type: 'jsx',
20
+ value: `<Tabs${groupIdProp}>\n<TabItem value="npm">`,
21
+ },
22
+ {
23
+ type: node.type,
24
+ lang: node.lang,
25
+ value: npmCode,
26
+ },
27
+ {
28
+ type: 'jsx',
29
+ value: '</TabItem>\n<TabItem value="yarn" label="Yarn">',
30
+ },
31
+ {
32
+ type: node.type,
33
+ lang: node.lang,
34
+ value: yarnCode,
35
+ },
36
+ {
37
+ type: 'jsx',
38
+ value: '</TabItem>\n</Tabs>',
39
+ },
40
+ ];
41
+ };
42
+ const isImport = (node) => node.type === 'import';
43
+ const isParent = (node) => Array.isArray(node.children);
44
+ const matchNode = (node) => node.type === 'code' && node.meta === 'npm2yarn';
45
+ const nodeForImport = {
46
+ type: 'import',
47
+ value: "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';",
48
+ };
49
+ const plugin = (options = {}) => {
50
+ const { sync = false } = options;
51
+ let transformed = false;
52
+ let alreadyImported = false;
53
+ const transformer = (root) => {
54
+ (0, unist_util_visit_1.default)(root, (node) => {
55
+ if (isImport(node) && node.value.includes('@theme/Tabs')) {
56
+ alreadyImported = true;
57
+ }
58
+ if (isParent(node)) {
59
+ let index = 0;
60
+ while (index < node.children.length) {
61
+ const child = node.children[index];
62
+ if (matchNode(child)) {
63
+ const result = transformNode(child, sync);
64
+ node.children.splice(index, 1, ...result);
65
+ index += result.length;
66
+ transformed = true;
67
+ }
68
+ else {
69
+ index += 1;
70
+ }
71
+ }
72
+ }
73
+ });
74
+ if (transformed && !alreadyImported) {
75
+ root.children.unshift(nodeForImport);
76
+ }
77
+ };
78
+ return transformer;
79
+ };
80
+ module.exports = plugin;
81
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAKH,gFAAqC;AACrC,sEAAoC;AAMpC,yCAAyC;AACzC,MAAM,gBAAg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
1
1
  {
2
2
  "name": "@docusaurus/remark-plugin-npm2yarn",
3
- "version": "2.0.0-beta.15d451942",
3
+ "version": "2.0.0-beta.18",
4
4
  "description": "Remark plugin for converting npm commands to Yarn commands as tabs.",
5
- "main": "src/index.js",
5
+ "main": "lib/index.js",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "watch": "tsc --watch"
12
+ },
9
13
  "repository": {
10
14
  "type": "git",
11
15
  "url": "https://github.com/facebook/docusaurus.git",
@@ -13,19 +17,18 @@
13
17
  },
14
18
  "license": "MIT",
15
19
  "dependencies": {
16
- "npm-to-yarn": "^1.0.1"
20
+ "npm-to-yarn": "^1.0.1",
21
+ "tslib": "^2.3.1",
22
+ "unist-util-visit": "^2.0.2"
17
23
  },
18
24
  "devDependencies": {
25
+ "@types/mdast": "^3.0.10",
19
26
  "remark": "^12.0.0",
20
27
  "remark-mdx": "^1.6.21",
21
28
  "to-vfile": "^6.0.0"
22
29
  },
23
- "peerDependencies": {
24
- "react": "^16.8.4 || ^17.0.0",
25
- "react-dom": "^16.8.4 || ^17.0.0"
26
- },
27
30
  "engines": {
28
- "node": ">=12.13.0"
31
+ "node": ">=14"
29
32
  },
30
- "gitHead": "dfceae8bd3e8b42ba8fc14f1449d861046c26143"
33
+ "gitHead": "1a945d06993d53376e61bed2c942799fe07dc336"
31
34
  }
package/src/index.ts ADDED
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import type {Code, Content, Literal} from 'mdast';
9
+ import type {Plugin, Transformer} from 'unified';
10
+ import type {Node, Parent} from 'unist';
11
+ import visit from 'unist-util-visit';
12
+ import npmToYarn from 'npm-to-yarn';
13
+
14
+ interface PluginOptions {
15
+ sync?: boolean;
16
+ }
17
+
18
+ // E.g. global install: 'npm i' -> 'yarn'
19
+ const convertNpmToYarn = (npmCode: string) => npmToYarn(npmCode, 'yarn');
20
+
21
+ const transformNode = (node: Code, isSync: boolean) => {
22
+ const groupIdProp = isSync ? ' groupId="npm2yarn"' : '';
23
+ const npmCode = node.value;
24
+ const yarnCode = convertNpmToYarn(node.value);
25
+ return [
26
+ {
27
+ type: 'jsx',
28
+ value: `<Tabs${groupIdProp}>\n<TabItem value="npm">`,
29
+ },
30
+ {
31
+ type: node.type,
32
+ lang: node.lang,
33
+ value: npmCode,
34
+ },
35
+ {
36
+ type: 'jsx',
37
+ value: '</TabItem>\n<TabItem value="yarn" label="Yarn">',
38
+ },
39
+ {
40
+ type: node.type,
41
+ lang: node.lang,
42
+ value: yarnCode,
43
+ },
44
+ {
45
+ type: 'jsx',
46
+ value: '</TabItem>\n</Tabs>',
47
+ },
48
+ ] as Content[];
49
+ };
50
+
51
+ const isImport = (node: Node): node is Literal => node.type === 'import';
52
+ const isParent = (node: Node): node is Parent =>
53
+ Array.isArray((node as Parent).children);
54
+ const matchNode = (node: Node): node is Code =>
55
+ node.type === 'code' && (node as Code).meta === 'npm2yarn';
56
+ const nodeForImport: Literal = {
57
+ type: 'import',
58
+ value:
59
+ "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';",
60
+ };
61
+
62
+ const plugin: Plugin<[PluginOptions?]> = (options = {}) => {
63
+ const {sync = false} = options;
64
+ let transformed = false;
65
+ let alreadyImported = false;
66
+ const transformer: Transformer = (root) => {
67
+ visit(root, (node: Node) => {
68
+ if (isImport(node) && node.value.includes('@theme/Tabs')) {
69
+ alreadyImported = true;
70
+ }
71
+ if (isParent(node)) {
72
+ let index = 0;
73
+ while (index < node.children.length) {
74
+ const child = node.children[index]!;
75
+ if (matchNode(child)) {
76
+ const result = transformNode(child, sync);
77
+ node.children.splice(index, 1, ...result);
78
+ index += result.length;
79
+ transformed = true;
80
+ } else {
81
+ index += 1;
82
+ }
83
+ }
84
+ }
85
+ });
86
+ if (transformed && !alreadyImported) {
87
+ (root as Parent).children.unshift(nodeForImport);
88
+ }
89
+ };
90
+ return transformer;
91
+ };
92
+
93
+ // To continue supporting `require('npm2yarn')` without the `.default` ㄟ(▔,▔)ㄏ
94
+ // TODO change to export default after migrating to ESM
95
+ export = plugin;
@@ -1,74 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`npm2yarn plugin test: installation file 1`] = `
4
- "import Tabs from '@theme/Tabs';
5
- import TabItem from '@theme/TabItem';
6
-
7
- <Tabs defaultValue=\\"npm\\" values={[
8
- { label: 'npm', value: 'npm', },
9
- { label: 'Yarn', value: 'yarn', },
10
- ]}
11
- >
12
- <TabItem value=\\"npm\\">
13
-
14
- \`\`\`bash
15
- $ npm install --global docusaurus
16
- \`\`\`
17
-
18
- </TabItem>
19
- <TabItem value=\\"yarn\\">
20
-
21
- \`\`\`bash
22
- $ yarn add --global docusaurus
23
- \`\`\`
24
-
25
- </TabItem>
26
- </Tabs>
27
- "
28
- `;
29
-
30
- exports[`npm2yarn plugin test: language was not setted 1`] = `
31
- "\`\`\`npm2yarn
32
- npm install --save docusaurus-plugin-name
33
- \`\`\`
34
-
35
- \`\`\`bash
36
- npm install --save docusaurus-plugin-name
37
- \`\`\`
38
-
39
- \`\`\`shell
40
- npm install --save docusaurus-plugin-name
41
- \`\`\`
42
- "
43
- `;
44
-
45
- exports[`npm2yarn plugin test: plugin file 1`] = `
46
- "import Tabs from '@theme/Tabs';
47
- import TabItem from '@theme/TabItem';
48
-
49
- ## Installing a plugin
50
-
51
- A plugin is usually a npm package, so you install them like other npm packages using npm.
52
-
53
- <Tabs defaultValue=\\"npm\\" values={[
54
- { label: 'npm', value: 'npm', },
55
- { label: 'Yarn', value: 'yarn', },
56
- ]}
57
- >
58
- <TabItem value=\\"npm\\">
59
-
60
- \`\`\`bash
61
- npm install --save docusaurus-plugin-name
62
- \`\`\`
63
-
64
- </TabItem>
65
- <TabItem value=\\"yarn\\">
66
-
67
- \`\`\`bash
68
- yarn add docusaurus-plugin-name
69
- \`\`\`
70
-
71
- </TabItem>
72
- </Tabs>
73
- "
74
- `;
@@ -1,3 +0,0 @@
1
- ```bash npm2yarn
2
- $ npm install --global docusaurus
3
- ```
@@ -1,7 +0,0 @@
1
- ## Installing a plugin
2
-
3
- A plugin is usually a npm package, so you install them like other npm packages using npm.
4
-
5
- ```bash npm2yarn
6
- npm install --save docusaurus-plugin-name
7
- ```
@@ -1,11 +0,0 @@
1
- ```npm2yarn
2
- npm install --save docusaurus-plugin-name
3
- ```
4
-
5
- ```bash
6
- npm install --save docusaurus-plugin-name
7
- ```
8
-
9
- ```shell
10
- npm install --save docusaurus-plugin-name
11
- ```
@@ -1,53 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- /* eslint-disable no-param-reassign */
9
-
10
- import remark from 'remark';
11
- import npm2yarn from '../index';
12
- import vfile from 'to-vfile';
13
- import {join, relative} from 'path';
14
- import mdx from 'remark-mdx';
15
-
16
- const staticDir = `./${relative(process.cwd(), join(__dirname, 'fixtures'))}`;
17
-
18
- const processFixture = async (name, options) => {
19
- const path = join(__dirname, 'fixtures', `${name}.md`);
20
- const file = await vfile.read(path);
21
- const result = await remark()
22
- .use(mdx)
23
- .use(npm2yarn, {...options, filePath: path})
24
- .process(file);
25
-
26
- return result.toString();
27
- };
28
-
29
- describe('npm2yarn plugin', () => {
30
- test('test: installation file', async () => {
31
- const result = await processFixture('installation', {
32
- staticDir,
33
- });
34
-
35
- expect(result).toMatchSnapshot();
36
- });
37
-
38
- test('test: plugin file', async () => {
39
- const result = await processFixture('plugin', {
40
- staticDir,
41
- });
42
-
43
- expect(result).toMatchSnapshot();
44
- });
45
-
46
- test('test: language was not setted', async () => {
47
- const result = await processFixture('syntax-not-properly-set', {
48
- staticDir,
49
- });
50
-
51
- expect(result).toMatchSnapshot();
52
- });
53
- });
package/src/index.js DELETED
@@ -1,83 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- const npmToYarn = require('npm-to-yarn');
9
-
10
- // E.g. global install: 'npm i' -> 'yarn'
11
- const convertNpmToYarn = (npmCode) => npmToYarn(npmCode, 'yarn');
12
-
13
- const transformNode = (node, isSync) => {
14
- const groupIdProp = isSync ? 'groupId="npm2yarn" ' : '';
15
- const npmCode = node.value;
16
- const yarnCode = convertNpmToYarn(node.value);
17
- return [
18
- {
19
- type: 'jsx',
20
- value:
21
- `<Tabs defaultValue="npm" ${groupIdProp}` +
22
- `values={[
23
- { label: 'npm', value: 'npm', },
24
- { label: 'Yarn', value: 'yarn', },
25
- ]}
26
- >
27
- <TabItem value="npm">`,
28
- },
29
- {
30
- type: node.type,
31
- lang: node.lang,
32
- value: npmCode,
33
- },
34
- {
35
- type: 'jsx',
36
- value: '</TabItem>\n<TabItem value="yarn">',
37
- },
38
- {
39
- type: node.type,
40
- lang: node.lang,
41
- value: yarnCode,
42
- },
43
- {
44
- type: 'jsx',
45
- value: '</TabItem>\n</Tabs>',
46
- },
47
- ];
48
- };
49
-
50
- const matchNode = (node) => node.type === 'code' && node.meta === 'npm2yarn';
51
- const nodeForImport = {
52
- type: 'import',
53
- value:
54
- "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';",
55
- };
56
-
57
- module.exports = (options = {}) => {
58
- const {sync = false} = options;
59
- let transformed = false;
60
- const transformer = (node) => {
61
- if (matchNode(node)) {
62
- transformed = true;
63
- return transformNode(node, sync);
64
- }
65
- if (Array.isArray(node.children)) {
66
- let index = 0;
67
- while (index < node.children.length) {
68
- const result = transformer(node.children[index]);
69
- if (result) {
70
- node.children.splice(index, 1, ...result);
71
- index += result.length;
72
- } else {
73
- index += 1;
74
- }
75
- }
76
- }
77
- if (node.type === 'root' && transformed) {
78
- node.children.unshift(nodeForImport);
79
- }
80
- return null;
81
- };
82
- return transformer;
83
- };