-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(remark-plugin-npm2yarn): migrate package to TS #5931
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
71eba8d
refactor(remark-plugin-npm2yarn): migrate package to TS
duanwilliam 3f7cb76
fix(remark-plugin-npm2yarn): type as unified Plugin
duanwilliam c25a971
refactor(remark-plugin-npm2yarn): standardize code style with remark …
duanwilliam efe9d63
Use unist-util-visit
Josh-Cena 52d0bfb
Use export =
Josh-Cena e1a0e8d
Remove unneeded includes option
Josh-Cena 264c07a
Fix tests
Josh-Cena 075b4de
Migrate test to TS
Josh-Cena b894444
Make output look better
Josh-Cena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
packages/docusaurus-remark-plugin-npm2yarn/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* 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 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 PluginOptions { | ||
sync?: boolean; | ||
} | ||
|
||
// E.g. global install: 'npm i' -> 'yarn' | ||
const convertNpmToYarn = (npmCode: string) => npmToYarn(npmCode, 'yarn'); | ||
|
||
const transformNode = (node: Code, isSync: boolean): Parent => { | ||
const groupIdProp = isSync ? 'groupId="npm2yarn" ' : ''; | ||
const npmCode = node.value; | ||
const yarnCode = convertNpmToYarn(node.value); | ||
return { | ||
type: '', | ||
children: [ | ||
{ | ||
type: 'jsx', | ||
value: | ||
`<Tabs defaultValue="npm" ${groupIdProp}` + | ||
`values={[ | ||
{ label: 'npm', value: 'npm', }, | ||
{ label: 'Yarn', value: 'yarn', }, | ||
]} | ||
> | ||
<TabItem value="npm">`, | ||
}, | ||
{ | ||
type: node.type, | ||
lang: node.lang, | ||
value: npmCode, | ||
}, | ||
{ | ||
type: 'jsx', | ||
value: '</TabItem>\n<TabItem value="yarn">', | ||
}, | ||
{ | ||
type: node.type, | ||
lang: node.lang, | ||
value: yarnCode, | ||
}, | ||
{ | ||
type: 'jsx', | ||
value: '</TabItem>\n</Tabs>', | ||
}, | ||
] as Content[], | ||
}; | ||
}; | ||
|
||
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: Literal = { | ||
type: 'import', | ||
value: | ||
"import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';", | ||
}; | ||
|
||
const attacher: 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; | ||
} | ||
} | ||
} | ||
if (node.type === 'root' && transformed && !alreadyImported) { | ||
(node as Parent).children.unshift(nodeForImport); | ||
} | ||
return undefined; | ||
}; | ||
return transformer; | ||
}; | ||
|
||
module.exports = attacher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"incremental": true, | ||
"tsBuildInfoFile": "./lib/.tsbuildinfo", | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
}, | ||
"include": ["src/"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kinda hacky to return a dummy node with the list of children and then destructuring it but it's the minimal change to make
transformer
a valid unifiedTransformer
type. don't know if that's alright thoughThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually can we use
unist-util-visit
for this? I'm not sure why we don't nowLike this: https://github.com/smack0202/luxdocs/blob/main/src/remark/video.js