UNPKG

5.23 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ParagraphSplitter = void 0;
4var nodes_1 = require("../nodes");
5/**
6 * The ParagraphSplitter is a secondary stage that runs after the NodeParser has constructed
7 * the DocComment. It splits DocParagraph nodes into multiple paragraphs by looking for
8 * paragraph delimiters. Following CommonMark conventions, paragraphs are delimited by
9 * one or more blank lines. (These lines end with SoftBreak nodes.) The blank lines are
10 * not discarded. Instead, they are attached to the preceding paragraph. If the DocParagraph
11 * starts with blank lines, they are preserved to avoid creating a paragraph containing only
12 * whitespace.
13 */
14var ParagraphSplitter = /** @class */ (function () {
15 function ParagraphSplitter() {
16 }
17 /**
18 * Split all paragraphs belonging to the provided subtree.
19 */
20 ParagraphSplitter.splitParagraphs = function (node) {
21 if (node instanceof nodes_1.DocSection) {
22 ParagraphSplitter.splitParagraphsForSection(node);
23 // (We don't recurse here, since sections cannot contain subsections)
24 }
25 else {
26 for (var _i = 0, _a = node.getChildNodes(); _i < _a.length; _i++) {
27 var childNode = _a[_i];
28 ParagraphSplitter.splitParagraphs(childNode);
29 }
30 }
31 };
32 /**
33 * Split all paragraphs belonging to the provided DocSection.
34 */
35 ParagraphSplitter.splitParagraphsForSection = function (docSection) {
36 var inputNodes = docSection.nodes;
37 var outputNodes = [];
38 for (var _i = 0, inputNodes_1 = inputNodes; _i < inputNodes_1.length; _i++) {
39 var oldNode = inputNodes_1[_i];
40 if (oldNode.kind === nodes_1.DocNodeKind.Paragraph) {
41 ParagraphSplitter._splitParagraph(oldNode, outputNodes);
42 }
43 else {
44 outputNodes.push(oldNode);
45 }
46 }
47 // Replace the inputNodes with the outputNodes
48 docSection.clearNodes();
49 docSection.appendNodes(outputNodes);
50 };
51 ParagraphSplitter._splitParagraph = function (oldParagraph, outputNodes) {
52 var inputParagraphNodes = oldParagraph.nodes;
53 var currentParagraph = new nodes_1.DocParagraph({ configuration: oldParagraph.configuration });
54 outputNodes.push(currentParagraph);
55 var state = 0 /* Start */;
56 var currentIndex = 0;
57 while (currentIndex < inputParagraphNodes.length) {
58 // Scan forwards to the end of the line
59 var isBlankLine = true;
60 var lineEndIndex = currentIndex; // non-inclusive
61 do {
62 var node = inputParagraphNodes[lineEndIndex++];
63 if (node.kind === nodes_1.DocNodeKind.SoftBreak) {
64 break;
65 }
66 if (isBlankLine) {
67 if (!this._isWhitespace(node)) {
68 isBlankLine = false;
69 }
70 }
71 } while (lineEndIndex < inputParagraphNodes.length);
72 // At this point, the line and SoftBreak will be in inputParagraphNodes.slice(currentIndex, lineEndIndex)
73 switch (state) {
74 case 0 /* Start */:
75 // We're skipping any blank lines that start the first paragraph
76 if (!isBlankLine) {
77 state = 1 /* AwaitingTrailer */;
78 }
79 break;
80 case 1 /* AwaitingTrailer */:
81 // We already saw some content, so now we're looking for a blank line that starts the trailer
82 // at the end of this paragraph
83 if (isBlankLine) {
84 state = 2 /* ReadingTrailer */;
85 }
86 break;
87 case 2 /* ReadingTrailer */:
88 // We already found the trailer, so now we're looking for a non-blank line that will
89 // begin a new paragraph
90 if (!isBlankLine) {
91 // Start a new paragraph
92 currentParagraph = new nodes_1.DocParagraph({ configuration: oldParagraph.configuration });
93 outputNodes.push(currentParagraph);
94 state = 1 /* AwaitingTrailer */;
95 }
96 break;
97 }
98 // Append the line onto the current paragraph
99 for (var i = currentIndex; i < lineEndIndex; ++i) {
100 currentParagraph.appendNode(inputParagraphNodes[i]);
101 }
102 currentIndex = lineEndIndex;
103 }
104 };
105 ParagraphSplitter._isWhitespace = function (node) {
106 switch (node.kind) {
107 case nodes_1.DocNodeKind.PlainText:
108 var docPlainText = node;
109 return ParagraphSplitter._whitespaceRegExp.test(docPlainText.text);
110 default:
111 return false;
112 }
113 };
114 ParagraphSplitter._whitespaceRegExp = /^\s*$/;
115 return ParagraphSplitter;
116}());
117exports.ParagraphSplitter = ParagraphSplitter;
118//# sourceMappingURL=ParagraphSplitter.js.map
\No newline at end of file