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,20 @@
/**
* 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 { Parent, Node } from 'unist';
import type { PhrasingContent, Heading } from 'mdast';
import type { MdxJsxAttributeValueExpression, MdxJsxTextElement } from 'mdast-util-mdx';
/**
* Util to transform one node type to another node type
* The input node is mutated in place
* @param node the node to mutate
* @param newNode what the original node should become become
*/
export declare function transformNode<NewNode extends Node>(node: Node, newNode: NewNode): NewNode;
export declare function stringifyContent(node: Parent, toString: (param: unknown) => string): string;
export declare function toValue(node: PhrasingContent | Heading | MdxJsxTextElement, toString: (param: unknown) => string): string;
export declare function assetRequireAttributeValue(requireString: string, hash: string): MdxJsxAttributeValueExpression;
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAC,MAAM,EAAE,IAAI,EAAC,MAAM,OAAO,CAAC;AACxC,OAAO,KAAK,EAAC,eAAe,EAAE,OAAO,EAAC,MAAM,OAAO,CAAC;AACpD,OAAO,KAAK,EAEV,8BAA8B,EAC9B,iBAAiB,EAElB,MAAM,gBAAgB,CAAC;AAExB;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,OAAO,SAAS,IAAI,EAChD,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,OAAO,GACf,OAAO,CAUT;AAED,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GACnC,MAAM,CAIR;AA8BD,wBAAgB,OAAO,CACrB,IAAI,EAAE,eAAe,GAAG,OAAO,GAAG,iBAAiB,EACnD,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GACnC,MAAM,CAsBR;AAED,wBAAgB,0BAA0B,CACxC,aAAa,EAAE,MAAM,EACrB,IAAI,EAAE,MAAM,GACX,8BAA8B,CAkDhC"}

View File

@@ -0,0 +1,127 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.assetRequireAttributeValue = exports.toValue = exports.stringifyContent = exports.transformNode = void 0;
const tslib_1 = require("tslib");
const escape_html_1 = tslib_1.__importDefault(require("escape-html"));
/**
* Util to transform one node type to another node type
* The input node is mutated in place
* @param node the node to mutate
* @param newNode what the original node should become become
*/
function transformNode(node, newNode) {
Object.keys(node).forEach((key) => {
// @ts-expect-error: unsafe but ok
delete node[key];
});
Object.keys(newNode).forEach((key) => {
// @ts-expect-error: unsafe but ok
node[key] = newNode[key];
});
return node;
}
exports.transformNode = transformNode;
function stringifyContent(node, toString) {
return node.children
.map((item) => toValue(item, toString))
.join('');
}
exports.stringifyContent = stringifyContent;
// TODO This is really a workaround, and not super reliable
// For now we only support serializing tagName, className and content
// Can we implement the TOC with real JSX nodes instead of html strings later?
function mdxJsxTextElementToHtml(element, toString) {
const tag = element.name;
const attributes = element.attributes.filter((child) => child.type === 'mdxJsxAttribute');
const classAttribute = attributes.find((attr) => attr.name === 'className') ??
attributes.find((attr) => attr.name === 'class');
const classAttributeString = classAttribute
? `class="${(0, escape_html_1.default)(String(classAttribute.value))}"`
: ``;
const allAttributes = classAttributeString ? ` ${classAttributeString}` : '';
const content = stringifyContent(element, toString);
return `<${tag}${allAttributes}>${content}</${tag}>`;
}
function toValue(node, toString) {
switch (node.type) {
case 'mdxJsxTextElement': {
return mdxJsxTextElementToHtml(node, toString);
}
case 'text':
return (0, escape_html_1.default)(node.value);
case 'heading':
return stringifyContent(node, toString);
case 'inlineCode':
return `<code>${(0, escape_html_1.default)(node.value)}</code>`;
case 'emphasis':
return `<em>${stringifyContent(node, toString)}</em>`;
case 'strong':
return `<strong>${stringifyContent(node, toString)}</strong>`;
case 'delete':
return `<del>${stringifyContent(node, toString)}</del>`;
case 'link':
return stringifyContent(node, toString);
default:
return toString(node);
}
}
exports.toValue = toValue;
function assetRequireAttributeValue(requireString, hash) {
return {
type: 'mdxJsxAttributeValueExpression',
value: `require("${requireString}").default${hash && ` + '${hash}'`}`,
data: {
estree: {
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'BinaryExpression',
left: {
type: 'MemberExpression',
object: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'require',
},
arguments: [
{
type: 'Literal',
value: requireString,
raw: `"${requireString}"`,
},
],
optional: false,
},
property: {
type: 'Identifier',
name: 'default',
},
computed: false,
optional: false,
},
operator: '+',
right: {
type: 'Literal',
value: hash,
raw: `"${hash}"`,
},
},
},
],
sourceType: 'module',
comments: [],
},
},
};
}
exports.assetRequireAttributeValue = assetRequireAttributeValue;
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/utils/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,sEAAqC;AAUrC;;;;;GAKG;AACH,SAAgB,aAAa,CAC3B,IAAU,EACV,OAAgB;IAEhB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QAChC,kCAAkC;QAClC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;IACH,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACnC,kCAAkC;QAClC,IAAI,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC,CAAC,CAAC;IACH,OAAO,IAAe,CAAC;AACzB,CAAC;AAb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