From 71eba8da65e46237c4de57034310067a9e034a7e Mon Sep 17 00:00:00 2001 From: William Duan <38791932+duanwilliam@users.noreply.github.com> Date: Thu, 11 Nov 2021 19:24:13 -0800 Subject: [PATCH 1/9] refactor(remark-plugin-npm2yarn): migrate package to TS --- .../package.json | 7 +++- .../src/{index.js => index.ts} | 42 ++++++++++++------- .../src/types.d.ts | 28 +++++++++++++ .../tsconfig.json | 9 ++++ yarn.lock | 2 +- 5 files changed, 72 insertions(+), 16 deletions(-) rename packages/docusaurus-remark-plugin-npm2yarn/src/{index.js => index.ts} (59%) create mode 100644 packages/docusaurus-remark-plugin-npm2yarn/src/types.d.ts create mode 100644 packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json diff --git a/packages/docusaurus-remark-plugin-npm2yarn/package.json b/packages/docusaurus-remark-plugin-npm2yarn/package.json index f0b9c5c623c0..52b587115dbe 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/package.json +++ b/packages/docusaurus-remark-plugin-npm2yarn/package.json @@ -2,10 +2,14 @@ "name": "@docusaurus/remark-plugin-npm2yarn", "version": "2.0.0-beta.9", "description": "Remark plugin for converting npm commands to Yarn commands as tabs.", - "main": "src/index.js", + "main": "lib/index.js", "publishConfig": { "access": "public" }, + "scripts": { + "build": "tsc", + "watch": "tsc --watch" + }, "repository": { "type": "git", "url": "https://github.com/facebook/docusaurus.git", @@ -16,6 +20,7 @@ "npm-to-yarn": "^1.0.1" }, "devDependencies": { + "@types/mdast": "^3.0.10", "remark": "^12.0.0", "remark-mdx": "^1.6.21", "to-vfile": "^6.0.0" diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/index.js b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts similarity index 59% rename from packages/docusaurus-remark-plugin-npm2yarn/src/index.js rename to packages/docusaurus-remark-plugin-npm2yarn/src/index.ts index f0c6a2633441..e7460fda1f55 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/index.js +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts @@ -5,12 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -const npmToYarn = require('npm-to-yarn'); +import type {Code, Content, Import, Parent, Root} from 'mdast'; +import npmToYarn from 'npm-to-yarn'; + +interface Options { + sync?: boolean; +} // E.g. global install: 'npm i' -> 'yarn' -const convertNpmToYarn = (npmCode) => npmToYarn(npmCode, 'yarn'); +const convertNpmToYarn = (npmCode: string) => npmToYarn(npmCode, 'yarn'); -const transformNode = (node, isSync) => { +const transformNode = (node: Code, isSync: boolean): Content[] => { const groupIdProp = isSync ? 'groupId="npm2yarn" ' : ''; const npmCode = node.value; const yarnCode = convertNpmToYarn(node.value); @@ -47,31 +52,38 @@ const transformNode = (node, isSync) => { ]; }; -const matchNode = (node) => node.type === 'code' && node.meta === 'npm2yarn'; -const nodeForImport = { +type Node = Content | Root; + +const matchNode = (node: Node): node is Code => + node.type === 'code' && (node as Code).meta === 'npm2yarn'; +const nodeForImport: Import = { type: 'import', value: "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';", }; -module.exports = (options = {}) => { +const attacher = (options: Options = {}) => { const {sync = false} = options; let transformed = false; let alreadyImported = false; - const transformer = (node) => { - if (node.type === 'import' && node.value.includes('@theme/Tabs')) { + const transformer = (node: Node) => { + if ( + node.type === 'import' && + (node as Import).value.includes('@theme/Tabs') + ) { alreadyImported = true; + return undefined; } if (matchNode(node)) { transformed = true; return transformNode(node, sync); } - if (Array.isArray(node.children)) { + if (Array.isArray((node as Parent).children)) { let index = 0; - while (index < node.children.length) { - const result = transformer(node.children[index]); + while (index < (node as Parent).children.length) { + const result = transformer((node as Parent).children[index]); if (result) { - node.children.splice(index, 1, ...result); + (node as Parent).children.splice(index, 1, ...result); index += result.length; } else { index += 1; @@ -79,9 +91,11 @@ module.exports = (options = {}) => { } } if (node.type === 'root' && transformed && !alreadyImported) { - node.children.unshift(nodeForImport); + (node as Root).children.unshift(nodeForImport); } - return null; + return undefined; }; return transformer; }; + +module.exports = attacher; diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/types.d.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/types.d.ts new file mode 100644 index 000000000000..417056ee2d7e --- /dev/null +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/types.d.ts @@ -0,0 +1,28 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {Literal} from 'mdast'; + +declare module 'mdast' { + interface BlockContentMap { + jsx: JSX; + import: Import; + export: Export; + } + + export interface JSX extends Literal, Optional { + type: 'jsx'; + } + + export interface Import extends Literal { + type: 'import'; + } + + export interface Export extends Literal { + type: 'export'; + } +} diff --git a/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json b/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json new file mode 100644 index 000000000000..f5902ba1089b --- /dev/null +++ b/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo", + "rootDir": "src", + "outDir": "lib" + } +} diff --git a/yarn.lock b/yarn.lock index ac83f91ddb10..e442b79bbb79 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4262,7 +4262,7 @@ resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.174.tgz#b4b06b6eced9850eed6b6a8f1abdd0f5192803c1" integrity sha512-KMBLT6+g9qrGXpDt7ohjWPUD34WA/jasrtjTEHStF0NPdEwJ1N9SZ+4GaMVDeuk/y0+X5j9xFm6mNiXS7UoaLQ== -"@types/mdast@^3.0.0", "@types/mdast@^3.0.7": +"@types/mdast@^3.0.0", "@types/mdast@^3.0.10", "@types/mdast@^3.0.7": version "3.0.10" resolved "https://registry.yarnpkg.com/@types/mdast/-/mdast-3.0.10.tgz#4724244a82a4598884cbbe9bcfd73dff927ee8af" integrity sha512-W864tg/Osz1+9f4lrGTZpCSO5/z4608eUp19tbozkq2HJK6i3z1kT0H9tlADXuYIb1YYOBByU4Jsqkk75q48qA== From 3f7cb76afd84022c8c5992eb53d2cfe13ba652c1 Mon Sep 17 00:00:00 2001 From: William Duan <38791932+duanwilliam@users.noreply.github.com> Date: Thu, 11 Nov 2021 20:59:14 -0800 Subject: [PATCH 2/9] fix(remark-plugin-npm2yarn): type as unified Plugin --- .../package.json | 3 +- .../src/index.ts | 5 +- yarn.lock | 53 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-remark-plugin-npm2yarn/package.json b/packages/docusaurus-remark-plugin-npm2yarn/package.json index 52b587115dbe..c567f9659cb7 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/package.json +++ b/packages/docusaurus-remark-plugin-npm2yarn/package.json @@ -23,7 +23,8 @@ "@types/mdast": "^3.0.10", "remark": "^12.0.0", "remark-mdx": "^1.6.21", - "to-vfile": "^6.0.0" + "to-vfile": "^6.0.0", + "unified": "^10.1.0" }, "peerDependencies": { "react": "^16.8.4 || ^17.0.0", diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts index e7460fda1f55..948b48a0b812 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts @@ -6,6 +6,7 @@ */ import type {Code, Content, Import, Parent, Root} from 'mdast'; +import type {Plugin} from 'unified'; import npmToYarn from 'npm-to-yarn'; interface Options { @@ -62,7 +63,9 @@ const nodeForImport: Import = { "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';", }; -const attacher = (options: Options = {}) => { +const attacher: Plugin<[Options], Node, Content[]> = ( + options: Options = {}, +) => { const {sync = false} = options; let transformed = false; let alreadyImported = false; diff --git a/yarn.lock b/yarn.lock index e442b79bbb79..c57cfde6d9c7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5782,6 +5782,11 @@ bail@^1.0.0: resolved "https://registry.yarnpkg.com/bail/-/bail-1.0.5.tgz#b6fa133404a392cbc1f8c4bf63f5953351e7a776" integrity sha512-xFbRxM1tahm08yHBP16MMjVUAvDaBMD38zsM9EMAUN61omwLmKlOpB/Zku5QkjZ8TZ4vn53pj+t518cH0S03RQ== +bail@^2.0.0: + version "2.0.2" + resolved "https://registry.yarnpkg.com/bail/-/bail-2.0.2.tgz#d26f5cd8fe5d6f832a31517b9f7c356040ba6d5d" + integrity sha512-0xO6mYd7JB2YesxDKplafRpsiOzPt9V02ddPCLbY1xYGPOX24NTyN50qnUxgCPcSoYMhKpAuBTjQoRZCAkUDRw== + balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" @@ -11655,6 +11660,11 @@ is-plain-obj@^3.0.0: resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-3.0.0.tgz#af6f2ea14ac5a646183a5bbdb5baabbc156ad9d7" integrity sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA== +is-plain-obj@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-4.0.0.tgz#06c0999fd7574edf5a906ba5644ad0feb3a84d22" + integrity sha512-NXRbBtUdBioI73y/HmOhogw/U5msYPC9DAtGkJXeFcFWSFZw0mCUsPxk/snTuJHzNKA8kLBK4rH97RMB1BfCXw== + is-plain-object@^2.0.3, is-plain-object@^2.0.4: version "2.0.4" resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677" @@ -19340,6 +19350,11 @@ trough@^1.0.0: resolved "https://registry.yarnpkg.com/trough/-/trough-1.0.5.tgz#b8b639cefad7d0bb2abd37d433ff8293efa5f406" integrity sha512-rvuRbTarPXmMb79SmzEp8aqXNKcK+y0XaB298IXueQ8I2PsrATcPBCSPyK/dDNa2iWOhKlfNnOjdAOTBU/nkFA== +trough@^2.0.0: + version "2.0.2" + resolved "https://registry.yarnpkg.com/trough/-/trough-2.0.2.tgz#94a3aa9d5ce379fc561f6244905b3f36b7458d96" + integrity sha512-FnHq5sTMxC0sk957wHDzRnemFnNBvt/gSY99HzK8F7UP5WAbvP70yX5bd7CjEQkN+TjdxwI7g7lJ6podqrG2/w== + ts-essentials@^2.0.3: version "2.0.12" resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-2.0.12.tgz#c9303f3d74f75fa7528c3d49b80e089ab09d8745" @@ -19585,6 +19600,19 @@ unified@9.2.0: trough "^1.0.0" vfile "^4.0.0" +unified@^10.1.0: + version "10.1.0" + resolved "https://registry.yarnpkg.com/unified/-/unified-10.1.0.tgz#4e65eb38fc2448b1c5ee573a472340f52b9346fe" + integrity sha512-4U3ru/BRXYYhKbwXV6lU6bufLikoAavTwev89H5UxY8enDFaAT2VXmIXYNm6hb5oHPng/EXr77PVyDFcptbk5g== + dependencies: + "@types/unist" "^2.0.0" + bail "^2.0.0" + extend "^3.0.0" + is-buffer "^2.0.0" + is-plain-obj "^4.0.0" + trough "^2.0.0" + vfile "^5.0.0" + unified@^8.4.2: version "8.4.2" resolved "https://registry.yarnpkg.com/unified/-/unified-8.4.2.tgz#13ad58b4a437faa2751a4a4c6a16f680c500fff1" @@ -19725,6 +19753,13 @@ unist-util-stringify-position@^2.0.0: dependencies: "@types/unist" "^2.0.2" +unist-util-stringify-position@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/unist-util-stringify-position/-/unist-util-stringify-position-3.0.0.tgz#d517d2883d74d0daa0b565adc3d10a02b4a8cde9" + integrity sha512-SdfAl8fsDclywZpfMDTVDxA2V7LjtRDTOFd44wUJamgl6OlVngsqWjxvermMYf60elWHbxhuRCZml7AnuXCaSA== + dependencies: + "@types/unist" "^2.0.0" + unist-util-visit-parents@^3.0.0: version "3.1.1" resolved "https://registry.yarnpkg.com/unist-util-visit-parents/-/unist-util-visit-parents-3.1.1.tgz#65a6ce698f78a6b0f56aa0e88f13801886cdaef6" @@ -20039,6 +20074,14 @@ vfile-message@^2.0.0: "@types/unist" "^2.0.0" unist-util-stringify-position "^2.0.0" +vfile-message@^3.0.0: + version "3.0.2" + resolved "https://registry.yarnpkg.com/vfile-message/-/vfile-message-3.0.2.tgz#db7eaebe7fecb853010f2ef1664427f52baf8f74" + integrity sha512-UUjZYIOg9lDRwwiBAuezLIsu9KlXntdxwG+nXnjuQAHvBpcX3x0eN8h+I7TkY5nkCXj+cWVp4ZqebtGBvok8ww== + dependencies: + "@types/unist" "^2.0.0" + unist-util-stringify-position "^3.0.0" + vfile@^4.0.0: version "4.2.1" resolved "https://registry.yarnpkg.com/vfile/-/vfile-4.2.1.tgz#03f1dce28fc625c625bc6514350fbdb00fa9e624" @@ -20049,6 +20092,16 @@ vfile@^4.0.0: unist-util-stringify-position "^2.0.0" vfile-message "^2.0.0" +vfile@^5.0.0: + version "5.2.0" + resolved "https://registry.yarnpkg.com/vfile/-/vfile-5.2.0.tgz#a32a646ff9251c274dbe8675644a39031025b369" + integrity sha512-ftCpb6pU8Jrzcqku8zE6N3Gi4/RkDhRwEXSWudzZzA2eEOn/cBpsfk9aulCUR+j1raRSAykYQap9u6j6rhUaCA== + dependencies: + "@types/unist" "^2.0.0" + is-buffer "^2.0.0" + unist-util-stringify-position "^3.0.0" + vfile-message "^3.0.0" + vlq@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/vlq/-/vlq-1.0.1.tgz#c003f6e7c0b4c1edd623fd6ee50bbc0d6a1de468" From c25a9712ca0e53c4cf58c8d01e0995c2b2c534a7 Mon Sep 17 00:00:00 2001 From: William Duan <38791932+duanwilliam@users.noreply.github.com> Date: Thu, 11 Nov 2021 23:11:50 -0800 Subject: [PATCH 3/9] refactor(remark-plugin-npm2yarn): standardize code style with remark plugins in mdx-loader --- .../package.json | 3 +- .../src/index.ts | 93 ++++++++++--------- .../src/types.d.ts | 28 ------ .../tsconfig.json | 3 +- yarn.lock | 53 ----------- 5 files changed, 50 insertions(+), 130 deletions(-) delete mode 100644 packages/docusaurus-remark-plugin-npm2yarn/src/types.d.ts diff --git a/packages/docusaurus-remark-plugin-npm2yarn/package.json b/packages/docusaurus-remark-plugin-npm2yarn/package.json index c567f9659cb7..52b587115dbe 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/package.json +++ b/packages/docusaurus-remark-plugin-npm2yarn/package.json @@ -23,8 +23,7 @@ "@types/mdast": "^3.0.10", "remark": "^12.0.0", "remark-mdx": "^1.6.21", - "to-vfile": "^6.0.0", - "unified": "^10.1.0" + "to-vfile": "^6.0.0" }, "peerDependencies": { "react": "^16.8.4 || ^17.0.0", diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts index 948b48a0b812..496b51920fa2 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts @@ -5,75 +5,75 @@ * LICENSE file in the root directory of this source tree. */ -import type {Code, Content, Import, Parent, Root} from 'mdast'; -import type {Plugin} from 'unified'; +import type {Code, Content, Literal} from 'mdast'; +import type {Plugin, Transformer} from 'unified'; +import type {Node, Parent} from 'unist'; import npmToYarn from 'npm-to-yarn'; -interface Options { +interface PluginOptions { sync?: boolean; } // E.g. global install: 'npm i' -> 'yarn' const convertNpmToYarn = (npmCode: string) => npmToYarn(npmCode, 'yarn'); -const transformNode = (node: Code, isSync: boolean): Content[] => { +const transformNode = (node: Code, isSync: boolean): Parent => { const groupIdProp = isSync ? 'groupId="npm2yarn" ' : ''; const npmCode = node.value; const yarnCode = convertNpmToYarn(node.value); - return [ - { - type: 'jsx', - value: - ` `, - }, - { - type: node.type, - lang: node.lang, - value: npmCode, - }, - { - type: 'jsx', - value: '\n', - }, - { - type: node.type, - lang: node.lang, - value: yarnCode, - }, - { - type: 'jsx', - value: '\n', - }, - ]; + }, + { + type: node.type, + lang: node.lang, + value: npmCode, + }, + { + type: 'jsx', + value: '\n', + }, + { + type: node.type, + lang: node.lang, + value: yarnCode, + }, + { + type: 'jsx', + value: '\n', + }, + ] as Content[], + }; }; -type Node = Content | Root; - +const isImport = (node: Node): node is Literal => node.type === 'import'; +const isParent = (node: Node): node is Parent => + Array.isArray((node as Parent).children); const matchNode = (node: Node): node is Code => node.type === 'code' && (node as Code).meta === 'npm2yarn'; -const nodeForImport: Import = { +const nodeForImport: Literal = { type: 'import', value: "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';", }; -const attacher: Plugin<[Options], Node, Content[]> = ( - options: Options = {}, -) => { +const attacher: Plugin<[PluginOptions?]> = (options = {}) => { const {sync = false} = options; let transformed = false; let alreadyImported = false; - const transformer = (node: Node) => { - if ( - node.type === 'import' && - (node as Import).value.includes('@theme/Tabs') - ) { + const transformer: Transformer = (node, _) => { + if (isImport(node) && node.value.includes('@theme/Tabs')) { alreadyImported = true; return undefined; } @@ -81,12 +81,13 @@ const attacher: Plugin<[Options], Node, Content[]> = ( transformed = true; return transformNode(node, sync); } - if (Array.isArray((node as Parent).children)) { + if (isParent(node)) { let index = 0; - while (index < (node as Parent).children.length) { - const result = transformer((node as Parent).children[index]); + while (index < node.children.length) { + const {children: result} = + (transformer(node.children[index], _) as Parent) ?? {}; if (result) { - (node as Parent).children.splice(index, 1, ...result); + node.children.splice(index, 1, ...result); index += result.length; } else { index += 1; @@ -94,7 +95,7 @@ const attacher: Plugin<[Options], Node, Content[]> = ( } } if (node.type === 'root' && transformed && !alreadyImported) { - (node as Root).children.unshift(nodeForImport); + (node as Parent).children.unshift(nodeForImport); } return undefined; }; diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/types.d.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/types.d.ts deleted file mode 100644 index 417056ee2d7e..000000000000 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/types.d.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import {Literal} from 'mdast'; - -declare module 'mdast' { - interface BlockContentMap { - jsx: JSX; - import: Import; - export: Export; - } - - export interface JSX extends Literal, Optional { - type: 'jsx'; - } - - export interface Import extends Literal { - type: 'import'; - } - - export interface Export extends Literal { - type: 'export'; - } -} diff --git a/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json b/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json index f5902ba1089b..c85b6f78938c 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json +++ b/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json @@ -5,5 +5,6 @@ "tsBuildInfoFile": "./lib/.tsbuildinfo", "rootDir": "src", "outDir": "lib" - } + }, + "include": ["src/"] } diff --git a/yarn.lock b/yarn.lock index c57cfde6d9c7..e442b79bbb79 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5782,11 +5782,6 @@ bail@^1.0.0: resolved "https://registry.yarnpkg.com/bail/-/bail-1.0.5.tgz#b6fa133404a392cbc1f8c4bf63f5953351e7a776" integrity sha512-xFbRxM1tahm08yHBP16MMjVUAvDaBMD38zsM9EMAUN61omwLmKlOpB/Zku5QkjZ8TZ4vn53pj+t518cH0S03RQ== -bail@^2.0.0: - version "2.0.2" - resolved "https://registry.yarnpkg.com/bail/-/bail-2.0.2.tgz#d26f5cd8fe5d6f832a31517b9f7c356040ba6d5d" - integrity sha512-0xO6mYd7JB2YesxDKplafRpsiOzPt9V02ddPCLbY1xYGPOX24NTyN50qnUxgCPcSoYMhKpAuBTjQoRZCAkUDRw== - balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" @@ -11660,11 +11655,6 @@ is-plain-obj@^3.0.0: resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-3.0.0.tgz#af6f2ea14ac5a646183a5bbdb5baabbc156ad9d7" integrity sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA== -is-plain-obj@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-4.0.0.tgz#06c0999fd7574edf5a906ba5644ad0feb3a84d22" - integrity sha512-NXRbBtUdBioI73y/HmOhogw/U5msYPC9DAtGkJXeFcFWSFZw0mCUsPxk/snTuJHzNKA8kLBK4rH97RMB1BfCXw== - is-plain-object@^2.0.3, is-plain-object@^2.0.4: version "2.0.4" resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677" @@ -19350,11 +19340,6 @@ trough@^1.0.0: resolved "https://registry.yarnpkg.com/trough/-/trough-1.0.5.tgz#b8b639cefad7d0bb2abd37d433ff8293efa5f406" integrity sha512-rvuRbTarPXmMb79SmzEp8aqXNKcK+y0XaB298IXueQ8I2PsrATcPBCSPyK/dDNa2iWOhKlfNnOjdAOTBU/nkFA== -trough@^2.0.0: - version "2.0.2" - resolved "https://registry.yarnpkg.com/trough/-/trough-2.0.2.tgz#94a3aa9d5ce379fc561f6244905b3f36b7458d96" - integrity sha512-FnHq5sTMxC0sk957wHDzRnemFnNBvt/gSY99HzK8F7UP5WAbvP70yX5bd7CjEQkN+TjdxwI7g7lJ6podqrG2/w== - ts-essentials@^2.0.3: version "2.0.12" resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-2.0.12.tgz#c9303f3d74f75fa7528c3d49b80e089ab09d8745" @@ -19600,19 +19585,6 @@ unified@9.2.0: trough "^1.0.0" vfile "^4.0.0" -unified@^10.1.0: - version "10.1.0" - resolved "https://registry.yarnpkg.com/unified/-/unified-10.1.0.tgz#4e65eb38fc2448b1c5ee573a472340f52b9346fe" - integrity sha512-4U3ru/BRXYYhKbwXV6lU6bufLikoAavTwev89H5UxY8enDFaAT2VXmIXYNm6hb5oHPng/EXr77PVyDFcptbk5g== - dependencies: - "@types/unist" "^2.0.0" - bail "^2.0.0" - extend "^3.0.0" - is-buffer "^2.0.0" - is-plain-obj "^4.0.0" - trough "^2.0.0" - vfile "^5.0.0" - unified@^8.4.2: version "8.4.2" resolved "https://registry.yarnpkg.com/unified/-/unified-8.4.2.tgz#13ad58b4a437faa2751a4a4c6a16f680c500fff1" @@ -19753,13 +19725,6 @@ unist-util-stringify-position@^2.0.0: dependencies: "@types/unist" "^2.0.2" -unist-util-stringify-position@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/unist-util-stringify-position/-/unist-util-stringify-position-3.0.0.tgz#d517d2883d74d0daa0b565adc3d10a02b4a8cde9" - integrity sha512-SdfAl8fsDclywZpfMDTVDxA2V7LjtRDTOFd44wUJamgl6OlVngsqWjxvermMYf60elWHbxhuRCZml7AnuXCaSA== - dependencies: - "@types/unist" "^2.0.0" - unist-util-visit-parents@^3.0.0: version "3.1.1" resolved "https://registry.yarnpkg.com/unist-util-visit-parents/-/unist-util-visit-parents-3.1.1.tgz#65a6ce698f78a6b0f56aa0e88f13801886cdaef6" @@ -20074,14 +20039,6 @@ vfile-message@^2.0.0: "@types/unist" "^2.0.0" unist-util-stringify-position "^2.0.0" -vfile-message@^3.0.0: - version "3.0.2" - resolved "https://registry.yarnpkg.com/vfile-message/-/vfile-message-3.0.2.tgz#db7eaebe7fecb853010f2ef1664427f52baf8f74" - integrity sha512-UUjZYIOg9lDRwwiBAuezLIsu9KlXntdxwG+nXnjuQAHvBpcX3x0eN8h+I7TkY5nkCXj+cWVp4ZqebtGBvok8ww== - dependencies: - "@types/unist" "^2.0.0" - unist-util-stringify-position "^3.0.0" - vfile@^4.0.0: version "4.2.1" resolved "https://registry.yarnpkg.com/vfile/-/vfile-4.2.1.tgz#03f1dce28fc625c625bc6514350fbdb00fa9e624" @@ -20092,16 +20049,6 @@ vfile@^4.0.0: unist-util-stringify-position "^2.0.0" vfile-message "^2.0.0" -vfile@^5.0.0: - version "5.2.0" - resolved "https://registry.yarnpkg.com/vfile/-/vfile-5.2.0.tgz#a32a646ff9251c274dbe8675644a39031025b369" - integrity sha512-ftCpb6pU8Jrzcqku8zE6N3Gi4/RkDhRwEXSWudzZzA2eEOn/cBpsfk9aulCUR+j1raRSAykYQap9u6j6rhUaCA== - dependencies: - "@types/unist" "^2.0.0" - is-buffer "^2.0.0" - unist-util-stringify-position "^3.0.0" - vfile-message "^3.0.0" - vlq@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/vlq/-/vlq-1.0.1.tgz#c003f6e7c0b4c1edd623fd6ee50bbc0d6a1de468" From efe9d6358677771848df71ea3ad9fadd72381425 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Fri, 12 Nov 2021 20:25:26 +0800 Subject: [PATCH 4/9] Use unist-util-visit --- .../package.json | 3 +- .../src/index.ts | 108 ++++++++---------- website/docusaurus.config.js | 2 +- 3 files changed, 51 insertions(+), 62 deletions(-) diff --git a/packages/docusaurus-remark-plugin-npm2yarn/package.json b/packages/docusaurus-remark-plugin-npm2yarn/package.json index 52b587115dbe..b5de28b4b078 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/package.json +++ b/packages/docusaurus-remark-plugin-npm2yarn/package.json @@ -17,7 +17,8 @@ }, "license": "MIT", "dependencies": { - "npm-to-yarn": "^1.0.1" + "npm-to-yarn": "^1.0.1", + "unist-util-visit": "^2.0.2" }, "devDependencies": { "@types/mdast": "^3.0.10", diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts index 496b51920fa2..0ea8c5b9cff6 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts @@ -8,6 +8,7 @@ import type {Code, Content, Literal} from 'mdast'; import type {Plugin, Transformer} from 'unified'; import type {Node, Parent} from 'unist'; +import visit from 'unist-util-visit'; import npmToYarn from 'npm-to-yarn'; interface PluginOptions { @@ -17,44 +18,34 @@ interface PluginOptions { // E.g. global install: 'npm i' -> 'yarn' const convertNpmToYarn = (npmCode: string) => npmToYarn(npmCode, 'yarn'); -const transformNode = (node: Code, isSync: boolean): Parent => { +const transformNode = (node: Code, isSync: boolean) => { const groupIdProp = isSync ? 'groupId="npm2yarn" ' : ''; const npmCode = node.value; const yarnCode = convertNpmToYarn(node.value); - return { - type: '', - children: [ - { - type: 'jsx', - value: - ` -`, - }, - { - type: node.type, - lang: node.lang, - value: npmCode, - }, - { - type: 'jsx', - value: '\n', - }, - { - type: node.type, - lang: node.lang, - value: yarnCode, - }, - { - type: 'jsx', - value: '\n', - }, - ] as Content[], - }; + return [ + { + type: 'jsx', + value: `\n`, + }, + { + type: node.type, + lang: node.lang, + value: npmCode, + }, + { + type: 'jsx', + value: '\n', + }, + { + type: node.type, + lang: node.lang, + value: yarnCode, + }, + { + type: 'jsx', + value: '\n', + }, + ] as Content[]; }; const isImport = (node: Node): node is Literal => node.type === 'import'; @@ -68,38 +59,35 @@ const nodeForImport: Literal = { "import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';", }; -const attacher: Plugin<[PluginOptions?]> = (options = {}) => { +const plugin: Plugin<[PluginOptions?]> = (options = {}) => { const {sync = false} = options; let transformed = false; let alreadyImported = false; - const transformer: Transformer = (node, _) => { - if (isImport(node) && node.value.includes('@theme/Tabs')) { - alreadyImported = true; - return undefined; - } - if (matchNode(node)) { - transformed = true; - return transformNode(node, sync); - } - if (isParent(node)) { - let index = 0; - while (index < node.children.length) { - const {children: result} = - (transformer(node.children[index], _) as Parent) ?? {}; - if (result) { - node.children.splice(index, 1, ...result); - index += result.length; - } else { - index += 1; + const transformer: Transformer = (root) => { + visit(root, (node: Node) => { + if (isImport(node) && node.value.includes('@theme/Tabs')) { + alreadyImported = true; + } + if (isParent(node)) { + let index = 0; + while (index < node.children.length) { + const child = node.children[index]; + if (matchNode(child)) { + const result = transformNode(child, sync); + node.children.splice(index, 1, ...result); + index += result.length; + transformed = true; + } else { + index += 1; + } } } + }); + if (transformed && !alreadyImported) { + (root as Parent).children.unshift(nodeForImport); } - if (node.type === 'root' && transformed && !alreadyImported) { - (node as Parent).children.unshift(nodeForImport); - } - return undefined; }; return transformer; }; -module.exports = attacher; +export default plugin; diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 6d4256aaac98..b6f18c485bad 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -13,7 +13,7 @@ const katex = require('rehype-katex'); const VersionsArchived = require('./versionsArchived.json'); const {dogfoodingPluginInstances} = require('./_dogfooding/dogfooding.config'); const FeatureRequestsPlugin = require('./src/featureRequests/FeatureRequestsPlugin'); -const npm2yarn = require('@docusaurus/remark-plugin-npm2yarn'); +const npm2yarn = require('@docusaurus/remark-plugin-npm2yarn').default; // eslint-disable-next-line import/no-extraneous-dependencies const lightTheme = require('prism-react-renderer/themes/github'); // eslint-disable-next-line import/no-extraneous-dependencies From 52d0bfbab0c79ef22658f223fe13d7714cd08ebd Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Fri, 12 Nov 2021 20:27:13 +0800 Subject: [PATCH 5/9] Use export = --- packages/docusaurus-remark-plugin-npm2yarn/src/index.ts | 2 +- website/docusaurus.config.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts index 0ea8c5b9cff6..0dc177357133 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts @@ -90,4 +90,4 @@ const plugin: Plugin<[PluginOptions?]> = (options = {}) => { return transformer; }; -export default plugin; +export = plugin; diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index b6f18c485bad..6d4256aaac98 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -13,7 +13,7 @@ const katex = require('rehype-katex'); const VersionsArchived = require('./versionsArchived.json'); const {dogfoodingPluginInstances} = require('./_dogfooding/dogfooding.config'); const FeatureRequestsPlugin = require('./src/featureRequests/FeatureRequestsPlugin'); -const npm2yarn = require('@docusaurus/remark-plugin-npm2yarn').default; +const npm2yarn = require('@docusaurus/remark-plugin-npm2yarn'); // eslint-disable-next-line import/no-extraneous-dependencies const lightTheme = require('prism-react-renderer/themes/github'); // eslint-disable-next-line import/no-extraneous-dependencies From e1a0e8da041fb86007d7bc967d034aebba483d40 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Fri, 12 Nov 2021 20:28:10 +0800 Subject: [PATCH 6/9] Remove unneeded includes option --- packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json b/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json index c85b6f78938c..f5902ba1089b 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json +++ b/packages/docusaurus-remark-plugin-npm2yarn/tsconfig.json @@ -5,6 +5,5 @@ "tsBuildInfoFile": "./lib/.tsbuildinfo", "rootDir": "src", "outDir": "lib" - }, - "include": ["src/"] + } } From 264c07a3c559a870c461874e09fc0a8095d984b6 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Fri, 12 Nov 2021 20:43:48 +0800 Subject: [PATCH 7/9] Fix tests --- .../src/__tests__/index.test.js | 4 +++- packages/docusaurus-remark-plugin-npm2yarn/src/index.ts | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.js b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.js index 77eca4cbd787..5ef5826f7124 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.js +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.js @@ -6,7 +6,9 @@ */ import remark from 'remark'; -import npm2yarn from '../index'; +// import from the transpiled lib because Babel can't transpile `export =` syntax +// TODO change to `../index` after migrating to ESM +import npm2yarn from '../../lib/index'; import vfile from 'to-vfile'; import {join, relative} from 'path'; import mdx from 'remark-mdx'; diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts index 0dc177357133..da955f99bffd 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts @@ -90,4 +90,6 @@ const plugin: Plugin<[PluginOptions?]> = (options = {}) => { return transformer; }; +// To continue supporting `require('npm2yarn')` without the `.default` ㄟ(▔,▔)ㄏ +// TODO change to export default after migrating to ESM export = plugin; From 075b4dee01734fab028ade47d66c086400ca29ee Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Fri, 12 Nov 2021 20:56:10 +0800 Subject: [PATCH 8/9] Migrate test to TS --- ...{index.test.js.snap => index.test.ts.snap} | 32 +++++-------------- .../{index.test.js => index.test.ts} | 5 ++- 2 files changed, 12 insertions(+), 25 deletions(-) rename packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/{index.test.js.snap => index.test.ts.snap} (73%) rename packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/{index.test.js => index.test.ts} (94%) diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.js.snap b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap similarity index 73% rename from packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.js.snap rename to packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap index b58a9850649c..4b440b466817 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.js.snap +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap @@ -5,11 +5,7 @@ exports[`npm2yarn plugin test: already imported tabs components above are not re import TabItem from '@theme/TabItem'; - + \`\`\`bash @@ -17,7 +13,7 @@ import TabItem from '@theme/TabItem'; \`\`\` - + \`\`\`bash $ yarn add --global docusaurus @@ -29,11 +25,7 @@ import TabItem from '@theme/TabItem'; `; exports[`npm2yarn plugin test: already imported tabs components below are not re-imported 1`] = ` -" +" \`\`\`bash @@ -41,7 +33,7 @@ exports[`npm2yarn plugin test: already imported tabs components below are not re \`\`\` - + \`\`\`bash $ yarn add --global docusaurus @@ -60,11 +52,7 @@ exports[`npm2yarn plugin test: installation file 1`] = ` "import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; - + \`\`\`bash @@ -72,7 +60,7 @@ import TabItem from '@theme/TabItem'; \`\`\` - + \`\`\`bash $ yarn add --global docusaurus @@ -106,11 +94,7 @@ import TabItem from '@theme/TabItem'; A plugin is usually a npm package, so you install them like other npm packages using npm. - + \`\`\`bash @@ -118,7 +102,7 @@ npm install --save docusaurus-plugin-name \`\`\` - + \`\`\`bash yarn add docusaurus-plugin-name diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.js b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.ts similarity index 94% rename from packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.js rename to packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.ts index 5ef5826f7124..3cab9ab43112 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.js +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.ts @@ -15,7 +15,10 @@ import mdx from 'remark-mdx'; const staticDir = `./${relative(process.cwd(), join(__dirname, 'fixtures'))}`; -const processFixture = async (name, options) => { +const processFixture = async ( + name: string, + options: {sync?: boolean; staticDir: string}, +) => { const path = join(__dirname, 'fixtures', `${name}.md`); const file = await vfile.read(path); const result = await remark() From b894444915620ac09d501c50c3cdd50983c761d7 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Fri, 12 Nov 2021 21:03:12 +0800 Subject: [PATCH 9/9] Make output look better --- .../src/__tests__/__snapshots__/index.test.ts.snap | 8 ++++---- packages/docusaurus-remark-plugin-npm2yarn/src/index.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap index 4b440b466817..290c9d567c58 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap @@ -5,7 +5,7 @@ exports[`npm2yarn plugin test: already imported tabs components above are not re import TabItem from '@theme/TabItem'; - + \`\`\`bash @@ -25,7 +25,7 @@ import TabItem from '@theme/TabItem'; `; exports[`npm2yarn plugin test: already imported tabs components below are not re-imported 1`] = ` -" +" \`\`\`bash @@ -52,7 +52,7 @@ exports[`npm2yarn plugin test: installation file 1`] = ` "import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; - + \`\`\`bash @@ -94,7 +94,7 @@ import TabItem from '@theme/TabItem'; A plugin is usually a npm package, so you install them like other npm packages using npm. - + \`\`\`bash diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts index da955f99bffd..7f80ed367824 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts @@ -19,13 +19,13 @@ interface PluginOptions { const convertNpmToYarn = (npmCode: string) => npmToYarn(npmCode, 'yarn'); const transformNode = (node: Code, isSync: boolean) => { - const groupIdProp = isSync ? 'groupId="npm2yarn" ' : ''; + const groupIdProp = isSync ? ' groupId="npm2yarn"' : ''; const npmCode = node.value; const yarnCode = convertNpmToYarn(node.value); return [ { type: 'jsx', - value: `\n`, + value: `\n`, }, { type: node.type,