This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,10 @@
type Plugin = any;
export type AdmonitionOptions = {
keywords: string[];
extendDefaults: boolean;
};
export declare const DefaultAdmonitionOptions: AdmonitionOptions;
export declare function normalizeAdmonitionOptions(providedOptions: Partial<AdmonitionOptions> | true): AdmonitionOptions;
declare const plugin: Plugin;
export default plugin;
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/admonitions/index.ts"],"names":[],"mappings":"AAiBA,KAAK,MAAM,GAAG,GAAG,CAAC;AAElB,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,cAAc,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,iBAatC,CAAC;AAEF,wBAAgB,0BAA0B,CACxC,eAAe,EAAE,OAAO,CAAC,iBAAiB,CAAC,GAAG,IAAI,GACjD,iBAAiB,CAiBnB;AA6BD,QAAA,MAAM,MAAM,EAAE,MAuDb,CAAC;AAEF,eAAe,MAAM,CAAC"}

View File

@@ -0,0 +1,98 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeAdmonitionOptions = exports.DefaultAdmonitionOptions = void 0;
exports.DefaultAdmonitionOptions = {
keywords: [
'secondary',
'info',
'success',
'danger',
'note',
'tip',
'warning',
'important',
'caution',
],
extendDefaults: true,
};
function normalizeAdmonitionOptions(providedOptions) {
if (providedOptions === true) {
return exports.DefaultAdmonitionOptions;
}
const options = { ...exports.DefaultAdmonitionOptions, ...providedOptions };
// By default it makes more sense to append keywords to the default ones
// Adding custom keywords is more common than disabling existing ones
if (options.extendDefaults) {
options.keywords = [
...exports.DefaultAdmonitionOptions.keywords,
...options.keywords,
];
}
return options;
}
exports.normalizeAdmonitionOptions = normalizeAdmonitionOptions;
function parseDirective(directive) {
const hasDirectiveLabel =
// @ts-expect-error: fine
directive.children?.[0]?.data?.directiveLabel === true;
if (hasDirectiveLabel) {
const [directiveLabel, ...contentNodes] = directive.children;
return { directiveLabel: directiveLabel, contentNodes };
}
return { directiveLabel: undefined, contentNodes: directive.children };
}
function getTextOnlyTitle(directiveLabel) {
const isTextOnlyTitle = directiveLabel?.children?.length === 1 &&
directiveLabel?.children?.[0]?.type === 'text';
return isTextOnlyTitle
? // @ts-expect-error: todo type
directiveLabel?.children?.[0].value
: undefined;
}
const plugin = function plugin(optionsInput = {}) {
const { keywords } = normalizeAdmonitionOptions(optionsInput);
return async (root) => {
const { visit } = await import('unist-util-visit');
visit(root, (node) => {
if (node.type === 'containerDirective') {
const directive = node;
const isAdmonition = keywords.includes(directive.name);
if (!isAdmonition) {
return;
}
const { directiveLabel, contentNodes } = parseDirective(directive);
const textOnlyTitle = directive.attributes?.title ??
(directiveLabel ? getTextOnlyTitle(directiveLabel) : undefined);
// Transform the mdast directive node to a hast admonition node
// See https://github.com/syntax-tree/mdast-util-to-hast#fields-on-nodes
// TODO in MDX v2 we should transform the whole directive to
// mdxJsxFlowElement instead of using hast
directive.data = {
hName: 'admonition',
hProperties: {
...(textOnlyTitle && { title: textOnlyTitle }),
type: directive.name,
},
};
directive.children = contentNodes;
// TODO legacy MDX v1 <mdxAdmonitionTitle> workaround
// v1: not possible to inject complex JSX elements as props
// v2: now possible: use a mdxJsxFlowElement element
if (directiveLabel && !textOnlyTitle) {
const complexTitleNode = {
type: 'mdxAdmonitionTitle',
data: {
hName: 'mdxAdmonitionTitle',
hProperties: {},
},
children: directiveLabel.children,
};
// @ts-expect-error: invented node type
directive.children.unshift(complexTitleNode);
}
}
});
};
};
exports.default = plugin;
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/admonitions/index.ts"],"names":[],"mappings":";;;AAwBa,QAAA,wBAAwB,GAAs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