Initial commit

This commit is contained in:
yuding
2025-12-03 12:00:46 +08:00
commit 5763b764a3
5365 changed files with 1483113 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
import type { DocParagraph } from '../nodes';
/**
* Helper functions that transform DocNode trees.
*/
export declare class DocNodeTransforms {
/**
* trimSpacesInParagraphNodes() collapses extra spacing characters from plain text nodes.
*
* @remarks
* This is useful when emitting HTML, where any number of spaces are equivalent
* to a single space. It's also useful when emitting Markdown, where spaces
* can be misinterpreted as an indented code block.
*
* For example, we might transform this:
*
* ```
* nodes: [
* { kind: PlainText, text: " Here are some " },
* { kind: SoftBreak }
* { kind: PlainText, text: " words" },
* { kind: SoftBreak }
* { kind: InlineTag, text: "{\@inheritDoc}" },
* { kind: PlainText, text: "to process." },
* { kind: PlainText, text: " " },
* { kind: PlainText, text: " " }
* ]
* ```
*
* ...to this:
*
* ```
* nodes: [
* { kind: PlainText, text: "Here are some " },
* { kind: PlainText, text: "words " },
* { kind: InlineTag, text: "{\@inheritDoc}" },
* { kind: PlainText, text: "to process." }
* ]
* ```
*
* Note that in this example, `"words "` is not merged with the preceding node because
* its DocPlainText.excerpt cannot span multiple lines.
*
* @param docParagraph - a DocParagraph containing nodes to be transformed
* @returns The transformed child nodes.
*/
static trimSpacesInParagraph(docParagraph: DocParagraph): DocParagraph;
}
//# sourceMappingURL=DocNodeTransforms.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"DocNodeTransforms.d.ts","sourceRoot":"","sources":["../../src/transforms/DocNodeTransforms.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C;;GAEG;AACH,qBAAa,iBAAiB;IAC5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;WACW,qBAAqB,CAAC,YAAY,EAAE,YAAY,GAAG,YAAY;CAG9E"}

View File

@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { TrimSpacesTransform } from './TrimSpacesTransform';
/**
* Helper functions that transform DocNode trees.
*/
export class DocNodeTransforms {
/**
* trimSpacesInParagraphNodes() collapses extra spacing characters from plain text nodes.
*
* @remarks
* This is useful when emitting HTML, where any number of spaces are equivalent
* to a single space. It's also useful when emitting Markdown, where spaces
* can be misinterpreted as an indented code block.
*
* For example, we might transform this:
*
* ```
* nodes: [
* { kind: PlainText, text: " Here are some " },
* { kind: SoftBreak }
* { kind: PlainText, text: " words" },
* { kind: SoftBreak }
* { kind: InlineTag, text: "{\@inheritDoc}" },
* { kind: PlainText, text: "to process." },
* { kind: PlainText, text: " " },
* { kind: PlainText, text: " " }
* ]
* ```
*
* ...to this:
*
* ```
* nodes: [
* { kind: PlainText, text: "Here are some " },
* { kind: PlainText, text: "words " },
* { kind: InlineTag, text: "{\@inheritDoc}" },
* { kind: PlainText, text: "to process." }
* ]
* ```
*
* Note that in this example, `"words "` is not merged with the preceding node because
* its DocPlainText.excerpt cannot span multiple lines.
*
* @param docParagraph - a DocParagraph containing nodes to be transformed
* @returns The transformed child nodes.
*/
static trimSpacesInParagraph(docParagraph) {
return TrimSpacesTransform.transform(docParagraph);
}
}
//# sourceMappingURL=DocNodeTransforms.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"DocNodeTransforms.js","sourceRoot":"","sources":["../../src/transforms/DocNodeTransforms.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAE3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAG5D;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAC5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;IACI,MAAM,CAAC,qBAAqB,CAAC,YAA0B;QAC5D,OAAO,mBAAmB,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;IACrD,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\r\n// See LICENSE in the project root for license information.\r\n\r\nimport { TrimSpacesTransform } from './TrimSpacesTransform';\r\nimport type { DocParagraph } from '../nodes';\r\n\r\n/**\r\n * Helper functions that transform DocNode trees.\r\n */\r\nexport class DocNodeTransforms {\r\n /**\r\n * trimSpacesInParagraphNodes() collapses extra spacing characters from plain text nodes.\r\n *\r\n * @remarks\r\n * This is useful when emitting HTML, where any number of spaces are equivalent\r\n * to a single space. It's also useful when emitting Markdown, where spaces\r\n * can be misinterpreted as an indented code block.\r\n *\r\n * For example, we might transform this:\r\n *\r\n * ```\r\n * nodes: [\r\n * { kind: PlainText, text: \" Here are some \" },\r\n * { kind: SoftBreak }\r\n * { kind: PlainText, text: \" words\" },\r\n * { kind: SoftBreak }\r\n * { kind: InlineTag, text: \"{\\@inheritDoc}\" },\r\n * { kind: PlainText, text: \"to process.\" },\r\n * { kind: PlainText, text: \" \" },\r\n * { kind: PlainText, text: \" \" }\r\n * ]\r\n * ```\r\n *\r\n * ...to this:\r\n *\r\n * ```\r\n * nodes: [\r\n * { kind: PlainText, text: \"Here are some \" },\r\n * { kind: PlainText, text: \"words \" },\r\n * { kind: InlineTag, text: \"{\\@inheritDoc}\" },\r\n * { kind: PlainText, text: \"to process.\" }\r\n * ]\r\n * ```\r\n *\r\n * Note that in this example, `\"words \"` is not merged with the preceding node because\r\n * its DocPlainText.excerpt cannot span multiple lines.\r\n *\r\n * @param docParagraph - a DocParagraph containing nodes to be transformed\r\n * @returns The transformed child nodes.\r\n */\r\n public static trimSpacesInParagraph(docParagraph: DocParagraph): DocParagraph {\r\n return TrimSpacesTransform.transform(docParagraph);\r\n }\r\n}\r\n"]}

View File

@@ -0,0 +1,8 @@
import { DocParagraph } from '../nodes';
/**
* Implementation of DocNodeTransforms.trimSpacesInParagraphNodes()
*/
export declare class TrimSpacesTransform {
static transform(docParagraph: DocParagraph): DocParagraph;
}
//# sourceMappingURL=TrimSpacesTransform.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"TrimSpacesTransform.d.ts","sourceRoot":"","sources":["../../src/transforms/TrimSpacesTransform.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAA2C,MAAM,UAAU,CAAC;AAEjF;;GAEG;AACH,qBAAa,mBAAmB;WAChB,SAAS,CAAC,YAAY,EAAE,YAAY,GAAG,YAAY;CA8FlE"}

View File

@@ -0,0 +1,84 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { DocParagraph, DocNodeKind, DocPlainText } from '../nodes';
/**
* Implementation of DocNodeTransforms.trimSpacesInParagraphNodes()
*/
export class TrimSpacesTransform {
static transform(docParagraph) {
const transformedNodes = [];
// Whether the next nonempty node to be added needs a space before it
let pendingSpace = false;
// The DocPlainText node that we're currently accumulating
const accumulatedTextChunks = [];
const accumulatedNodes = [];
// We always trim leading whitespace for a paragraph. This flag gets set to true
// as soon as nonempty content is encountered.
let finishedSkippingLeadingSpaces = false;
for (const node of docParagraph.nodes) {
switch (node.kind) {
case DocNodeKind.PlainText:
const docPlainText = node;
const text = docPlainText.text;
const startedWithSpace = /^\s/.test(text);
const endedWithSpace = /\s$/.test(text);
const collapsedText = text.replace(/\s+/g, ' ').trim();
if (startedWithSpace && finishedSkippingLeadingSpaces) {
pendingSpace = true;
}
if (collapsedText.length > 0) {
if (pendingSpace) {
accumulatedTextChunks.push(' ');
pendingSpace = false;
}
accumulatedTextChunks.push(collapsedText);
accumulatedNodes.push(node);
finishedSkippingLeadingSpaces = true;
}
if (endedWithSpace && finishedSkippingLeadingSpaces) {
pendingSpace = true;
}
break;
case DocNodeKind.SoftBreak:
if (finishedSkippingLeadingSpaces) {
pendingSpace = true;
}
accumulatedNodes.push(node);
break;
default:
if (pendingSpace) {
accumulatedTextChunks.push(' ');
pendingSpace = false;
}
// Push the accumulated text
if (accumulatedTextChunks.length > 0) {
// TODO: We should probably track the accumulatedNodes somehow, e.g. so we can map them back to the
// original excerpts. But we need a developer scenario before we can design this API.
transformedNodes.push(new DocPlainText({
configuration: docParagraph.configuration,
text: accumulatedTextChunks.join('')
}));
accumulatedTextChunks.length = 0;
accumulatedNodes.length = 0;
}
transformedNodes.push(node);
finishedSkippingLeadingSpaces = true;
}
}
// Push the accumulated text
if (accumulatedTextChunks.length > 0) {
transformedNodes.push(new DocPlainText({
configuration: docParagraph.configuration,
text: accumulatedTextChunks.join('')
}));
accumulatedTextChunks.length = 0;
accumulatedNodes.length = 0;
}
const transformedParagraph = new DocParagraph({
configuration: docParagraph.configuration
});
transformedParagraph.appendNodes(transformedNodes);
return transformedParagraph;
}
}
//# sourceMappingURL=TrimSpacesTransform.js.map

File diff suppressed because one or more lines are too long