-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,599 additions
and
232 deletions.
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 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 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 |
---|---|---|
@@ -1,60 +1,98 @@ | ||
import { strict as assert } from 'assert'; | ||
import { strict as assert } from 'node:assert'; | ||
|
||
import { visit } from 'unist-util-visit'; | ||
|
||
function needImport(nodes, text) { | ||
return !nodes.some( | ||
({ type, value }) => type === 'import' && value.includes(text), | ||
const placeholder = 'docusaurus-doc-card-list'; | ||
|
||
function isDirective({ type, children: [line] = [] }, _, parent) { | ||
return ( | ||
parent?.type === 'root' && | ||
type === 'paragraph' && | ||
line && | ||
((line.type === 'textDirective' && line.name === placeholder) || | ||
(line.type === 'text' && line.value === `:${placeholder}`)) | ||
); | ||
} | ||
|
||
function importStatement(value) { | ||
return { type: 'import', value }; | ||
} | ||
const importStatement = "import DocCardList from '@theme/DocCardList';"; | ||
|
||
function isDirective(placeholder) { | ||
return ({ type, children: [line] = [] }, _, parent) => { | ||
return ( | ||
parent?.type === 'root' && | ||
type === 'paragraph' && | ||
line?.type === 'text' && | ||
line?.value === placeholder | ||
); | ||
}; | ||
function matchStatement(value) { | ||
return ( | ||
value === importStatement || value === importStatement.replaceAll("'", '"') | ||
); | ||
} | ||
|
||
/* eslint-disable no-param-reassign */ | ||
export function docCardList({ | ||
placeholder = ':docusaurus-doc-card-list', | ||
} = {}) { | ||
assert( | ||
typeof placeholder === 'string', | ||
new TypeError('`placeholder` should be string'), | ||
); | ||
|
||
assert( | ||
placeholder.length > 5, | ||
new TypeError('`placeholder` should be longer than 5 characters'), | ||
); | ||
export function docCardList({ version = 2 } = {}) { | ||
assert([2, 3].includes(version), new TypeError('`version` should be 2 or 3')); | ||
|
||
return (tree) => { | ||
let haveDirective = false; | ||
|
||
visit(tree, isDirective(placeholder), (_, index, parent) => { | ||
visit(tree, isDirective, (_, index, parent) => { | ||
if (!haveDirective) { | ||
haveDirective = true; | ||
} | ||
|
||
parent.children[index] = { | ||
type: 'jsx', | ||
value: '<DocCardList />', | ||
...(version === 2 | ||
? { type: 'jsx', value: '<DocCardList />' } | ||
: { | ||
type: 'mdxJsxFlowElement', | ||
name: 'DocCardList', | ||
}), | ||
}; | ||
}); | ||
|
||
if (haveDirective && needImport(tree.children, '@theme/DocCardList')) { | ||
tree.children.unshift( | ||
importStatement("import DocCardList from '@theme/DocCardList';"), | ||
); | ||
if (haveDirective) { | ||
if ( | ||
version === 2 && | ||
!tree.children.some( | ||
({ type, value }) => type === 'import' && matchStatement(value), | ||
) | ||
) { | ||
tree.children.unshift({ | ||
type: 'import', | ||
value: importStatement, | ||
}); | ||
} | ||
|
||
if ( | ||
version === 3 && | ||
!tree.children.some( | ||
({ type, value }) => type === 'mdxjsEsm' && matchStatement(value), | ||
) | ||
) { | ||
tree.children.unshift({ | ||
type: 'mdxjsEsm', | ||
value: "import DocCardList from '@theme/DocCardList';", | ||
data: { | ||
estree: { | ||
type: 'Program', | ||
body: [ | ||
{ | ||
type: 'ImportDeclaration', | ||
specifiers: [ | ||
{ | ||
type: 'ImportDefaultSpecifier', | ||
local: { | ||
type: 'Identifier', | ||
name: 'DocCardList', | ||
}, | ||
}, | ||
], | ||
source: { | ||
type: 'Literal', | ||
value: '@theme/DocCardList', | ||
raw: "'@theme/DocCardList'", | ||
}, | ||
}, | ||
], | ||
sourceType: 'module', | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
}; | ||
} |
Oops, something went wrong.