@8btc/ppt-generator-mcp 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +129 -0
  2. package/dist/export-pptx.d.ts +6 -0
  3. package/dist/export-pptx.js +876 -0
  4. package/dist/generate-ppt-slides.d.ts +10 -0
  5. package/dist/generate-ppt-slides.js +581 -0
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +161 -0
  8. package/dist/ppt-generator.d.ts +39 -0
  9. package/dist/ppt-generator.js +110 -0
  10. package/dist/tpls/imgs.json +482 -0
  11. package/dist/tpls/tpl-1.json +7649 -0
  12. package/dist/tpls/tpl-2.json +7455 -0
  13. package/dist/tpls/tpl-3.json +8184 -0
  14. package/dist/tpls/tpl-4.json +8352 -0
  15. package/dist/types/outline.d.ts +36 -0
  16. package/dist/types/outline.js +2 -0
  17. package/dist/types/slide.d.ts +696 -0
  18. package/dist/types/slide.js +2 -0
  19. package/dist/utils/element.d.ts +94 -0
  20. package/dist/utils/element.js +239 -0
  21. package/dist/utils/htmlParser/format.d.ts +3 -0
  22. package/dist/utils/htmlParser/format.js +47 -0
  23. package/dist/utils/htmlParser/index.d.ts +4 -0
  24. package/dist/utils/htmlParser/index.js +15 -0
  25. package/dist/utils/htmlParser/lexer.d.ts +2 -0
  26. package/dist/utils/htmlParser/lexer.js +245 -0
  27. package/dist/utils/htmlParser/parser.d.ts +15 -0
  28. package/dist/utils/htmlParser/parser.js +117 -0
  29. package/dist/utils/htmlParser/stringify.d.ts +3 -0
  30. package/dist/utils/htmlParser/stringify.js +32 -0
  31. package/dist/utils/htmlParser/tags.d.ts +8 -0
  32. package/dist/utils/htmlParser/tags.js +50 -0
  33. package/dist/utils/htmlParser/types.d.ts +55 -0
  34. package/dist/utils/htmlParser/types.js +2 -0
  35. package/dist/utils/svg2Base64.d.ts +1 -0
  36. package/dist/utils/svg2Base64.js +58 -0
  37. package/dist/utils/svgPathParser.d.ts +120 -0
  38. package/dist/utils/svgPathParser.js +145 -0
  39. package/package.json +59 -0
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,94 @@
1
+ import type { PPTElement, PPTLineElement, Slide } from "../types/slide";
2
+ interface RotatedElementData {
3
+ left: number;
4
+ top: number;
5
+ width: number;
6
+ height: number;
7
+ rotate: number;
8
+ }
9
+ interface IdMap {
10
+ [id: string]: string;
11
+ }
12
+ /**
13
+ * 计算元素在画布中的矩形范围旋转后的新位置范围
14
+ * @param element 元素的位置大小和旋转角度信息
15
+ */
16
+ export declare const getRectRotatedRange: (element: RotatedElementData) => {
17
+ xRange: number[];
18
+ yRange: number[];
19
+ };
20
+ /**
21
+ * 计算元素在画布中的矩形范围旋转后的新位置与旋转之前位置的偏离距离
22
+ * @param element 元素的位置大小和旋转角度信息
23
+ */
24
+ export declare const getRectRotatedOffset: (element: RotatedElementData) => {
25
+ offsetX: number;
26
+ offsetY: number;
27
+ };
28
+ /**
29
+ * 计算元素在画布中的位置范围
30
+ * @param element 元素信息
31
+ */
32
+ export declare const getElementRange: (element: PPTElement) => {
33
+ minX: number;
34
+ maxX: number;
35
+ minY: number;
36
+ maxY: number;
37
+ };
38
+ /**
39
+ * 计算一组元素在画布中的位置范围
40
+ * @param elementList 一组元素信息
41
+ */
42
+ export declare const getElementListRange: (elementList: PPTElement[]) => {
43
+ minX: number;
44
+ maxX: number;
45
+ minY: number;
46
+ maxY: number;
47
+ };
48
+ /**
49
+ * 计算线条元素的长度
50
+ * @param element 线条元素
51
+ */
52
+ export declare const getLineElementLength: (element: PPTLineElement) => number;
53
+ export interface AlignLine {
54
+ value: number;
55
+ range: [number, number];
56
+ }
57
+ /**
58
+ * 将一组对齐吸附线进行去重:同位置的的多条对齐吸附线仅留下一条,取该位置所有对齐吸附线的最大值和最小值为新的范围
59
+ * @param lines 一组对齐吸附线信息
60
+ */
61
+ export declare const uniqAlignLines: (lines: AlignLine[]) => AlignLine[];
62
+ /**
63
+ * 以页面列表为基础,为每一个页面生成新的ID,并关联到旧ID形成一个字典
64
+ * 主要用于页面元素时,维持数据中各处页面ID原有的关系
65
+ * @param slides 页面列表
66
+ */
67
+ export declare const createSlideIdMap: (slides: Slide[]) => IdMap;
68
+ /**
69
+ * 以元素列表为基础,为每一个元素生成新的ID,并关联到旧ID形成一个字典
70
+ * 主要用于复制元素时,维持数据中各处元素ID原有的关系
71
+ * 例如:原本两个组合的元素拥有相同的groupId,复制后依然会拥有另一个相同的groupId
72
+ * @param elements 元素列表数据
73
+ */
74
+ export declare const createElementIdMap: (elements: PPTElement[]) => {
75
+ groupIdMap: IdMap;
76
+ elIdMap: IdMap;
77
+ };
78
+ /**
79
+ * 根据表格的主题色,获取对应用于配色的子颜色
80
+ * @param themeColor 主题色
81
+ */
82
+ export declare const getTableSubThemeColor: (themeColor: string) => string[];
83
+ /**
84
+ * 获取线条元素路径字符串
85
+ * @param element 线条元素
86
+ */
87
+ export declare const getLineElementPath: (element: PPTLineElement) => string;
88
+ /**
89
+ * 判断一个元素是否在可视范围内
90
+ * @param element 元素
91
+ * @param parent 父元素
92
+ */
93
+ export declare const isElementInViewport: (element: HTMLElement, parent: HTMLElement) => boolean;
94
+ export {};
@@ -0,0 +1,239 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isElementInViewport = exports.getLineElementPath = exports.getTableSubThemeColor = exports.createElementIdMap = exports.createSlideIdMap = exports.uniqAlignLines = exports.getLineElementLength = exports.getElementListRange = exports.getElementRange = exports.getRectRotatedOffset = exports.getRectRotatedRange = void 0;
7
+ const tinycolor2_1 = __importDefault(require("tinycolor2"));
8
+ const nanoid_1 = require("nanoid");
9
+ /**
10
+ * 计算元素在画布中的矩形范围旋转后的新位置范围
11
+ * @param element 元素的位置大小和旋转角度信息
12
+ */
13
+ const getRectRotatedRange = (element) => {
14
+ const { left, top, width, height, rotate = 0 } = element;
15
+ const radius = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / 2;
16
+ const auxiliaryAngle = (Math.atan(height / width) * 180) / Math.PI;
17
+ const tlbraRadian = ((180 - rotate - auxiliaryAngle) * Math.PI) / 180;
18
+ const trblaRadian = ((auxiliaryAngle - rotate) * Math.PI) / 180;
19
+ const middleLeft = left + width / 2;
20
+ const middleTop = top + height / 2;
21
+ const xAxis = [
22
+ middleLeft + radius * Math.cos(tlbraRadian),
23
+ middleLeft + radius * Math.cos(trblaRadian),
24
+ middleLeft - radius * Math.cos(tlbraRadian),
25
+ middleLeft - radius * Math.cos(trblaRadian),
26
+ ];
27
+ const yAxis = [
28
+ middleTop - radius * Math.sin(tlbraRadian),
29
+ middleTop - radius * Math.sin(trblaRadian),
30
+ middleTop + radius * Math.sin(tlbraRadian),
31
+ middleTop + radius * Math.sin(trblaRadian),
32
+ ];
33
+ return {
34
+ xRange: [Math.min(...xAxis), Math.max(...xAxis)],
35
+ yRange: [Math.min(...yAxis), Math.max(...yAxis)],
36
+ };
37
+ };
38
+ exports.getRectRotatedRange = getRectRotatedRange;
39
+ /**
40
+ * 计算元素在画布中的矩形范围旋转后的新位置与旋转之前位置的偏离距离
41
+ * @param element 元素的位置大小和旋转角度信息
42
+ */
43
+ const getRectRotatedOffset = (element) => {
44
+ const { xRange: originXRange, yRange: originYRange } = (0, exports.getRectRotatedRange)({
45
+ left: element.left,
46
+ top: element.top,
47
+ width: element.width,
48
+ height: element.height,
49
+ rotate: 0,
50
+ });
51
+ const { xRange: rotatedXRange, yRange: rotatedYRange } = (0, exports.getRectRotatedRange)({
52
+ left: element.left,
53
+ top: element.top,
54
+ width: element.width,
55
+ height: element.height,
56
+ rotate: element.rotate,
57
+ });
58
+ return {
59
+ offsetX: rotatedXRange[0] - originXRange[0],
60
+ offsetY: rotatedYRange[0] - originYRange[0],
61
+ };
62
+ };
63
+ exports.getRectRotatedOffset = getRectRotatedOffset;
64
+ /**
65
+ * 计算元素在画布中的位置范围
66
+ * @param element 元素信息
67
+ */
68
+ const getElementRange = (element) => {
69
+ let minX, maxX, minY, maxY;
70
+ if (element.type === "line") {
71
+ minX = element.left;
72
+ maxX = element.left + Math.max(element.start[0], element.end[0]);
73
+ minY = element.top;
74
+ maxY = element.top + Math.max(element.start[1], element.end[1]);
75
+ }
76
+ else if ("rotate" in element && element.rotate) {
77
+ const { left, top, width, height, rotate } = element;
78
+ const { xRange, yRange } = (0, exports.getRectRotatedRange)({
79
+ left,
80
+ top,
81
+ width,
82
+ height,
83
+ rotate,
84
+ });
85
+ minX = xRange[0];
86
+ maxX = xRange[1];
87
+ minY = yRange[0];
88
+ maxY = yRange[1];
89
+ }
90
+ else {
91
+ minX = element.left;
92
+ maxX = element.left + element.width;
93
+ minY = element.top;
94
+ maxY = element.top + element.height;
95
+ }
96
+ return { minX, maxX, minY, maxY };
97
+ };
98
+ exports.getElementRange = getElementRange;
99
+ /**
100
+ * 计算一组元素在画布中的位置范围
101
+ * @param elementList 一组元素信息
102
+ */
103
+ const getElementListRange = (elementList) => {
104
+ const leftValues = [];
105
+ const topValues = [];
106
+ const rightValues = [];
107
+ const bottomValues = [];
108
+ elementList.forEach((element) => {
109
+ const { minX, maxX, minY, maxY } = (0, exports.getElementRange)(element);
110
+ leftValues.push(minX);
111
+ topValues.push(minY);
112
+ rightValues.push(maxX);
113
+ bottomValues.push(maxY);
114
+ });
115
+ const minX = Math.min(...leftValues);
116
+ const maxX = Math.max(...rightValues);
117
+ const minY = Math.min(...topValues);
118
+ const maxY = Math.max(...bottomValues);
119
+ return { minX, maxX, minY, maxY };
120
+ };
121
+ exports.getElementListRange = getElementListRange;
122
+ /**
123
+ * 计算线条元素的长度
124
+ * @param element 线条元素
125
+ */
126
+ const getLineElementLength = (element) => {
127
+ const deltaX = element.end[0] - element.start[0];
128
+ const deltaY = element.end[1] - element.start[1];
129
+ const len = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
130
+ return len;
131
+ };
132
+ exports.getLineElementLength = getLineElementLength;
133
+ /**
134
+ * 将一组对齐吸附线进行去重:同位置的的多条对齐吸附线仅留下一条,取该位置所有对齐吸附线的最大值和最小值为新的范围
135
+ * @param lines 一组对齐吸附线信息
136
+ */
137
+ const uniqAlignLines = (lines) => {
138
+ const uniqLines = [];
139
+ lines.forEach((line) => {
140
+ const index = uniqLines.findIndex((_line) => _line.value === line.value);
141
+ if (index === -1)
142
+ uniqLines.push(line);
143
+ else {
144
+ const uniqLine = uniqLines[index];
145
+ const rangeMin = Math.min(uniqLine.range[0], line.range[0]);
146
+ const rangeMax = Math.max(uniqLine.range[1], line.range[1]);
147
+ const range = [rangeMin, rangeMax];
148
+ const _line = { value: line.value, range };
149
+ uniqLines[index] = _line;
150
+ }
151
+ });
152
+ return uniqLines;
153
+ };
154
+ exports.uniqAlignLines = uniqAlignLines;
155
+ /**
156
+ * 以页面列表为基础,为每一个页面生成新的ID,并关联到旧ID形成一个字典
157
+ * 主要用于页面元素时,维持数据中各处页面ID原有的关系
158
+ * @param slides 页面列表
159
+ */
160
+ const createSlideIdMap = (slides) => {
161
+ const slideIdMap = {};
162
+ for (const slide of slides) {
163
+ slideIdMap[slide.id] = (0, nanoid_1.nanoid)(10);
164
+ }
165
+ return slideIdMap;
166
+ };
167
+ exports.createSlideIdMap = createSlideIdMap;
168
+ /**
169
+ * 以元素列表为基础,为每一个元素生成新的ID,并关联到旧ID形成一个字典
170
+ * 主要用于复制元素时,维持数据中各处元素ID原有的关系
171
+ * 例如:原本两个组合的元素拥有相同的groupId,复制后依然会拥有另一个相同的groupId
172
+ * @param elements 元素列表数据
173
+ */
174
+ const createElementIdMap = (elements) => {
175
+ const groupIdMap = {};
176
+ const elIdMap = {};
177
+ for (const element of elements) {
178
+ const groupId = element.groupId;
179
+ if (groupId && !groupIdMap[groupId]) {
180
+ groupIdMap[groupId] = (0, nanoid_1.nanoid)(10);
181
+ }
182
+ elIdMap[element.id] = (0, nanoid_1.nanoid)(10);
183
+ }
184
+ return {
185
+ groupIdMap,
186
+ elIdMap,
187
+ };
188
+ };
189
+ exports.createElementIdMap = createElementIdMap;
190
+ /**
191
+ * 根据表格的主题色,获取对应用于配色的子颜色
192
+ * @param themeColor 主题色
193
+ */
194
+ const getTableSubThemeColor = (themeColor) => {
195
+ const rgba = (0, tinycolor2_1.default)(themeColor);
196
+ return [rgba.setAlpha(0.3).toRgbString(), rgba.setAlpha(0.1).toRgbString()];
197
+ };
198
+ exports.getTableSubThemeColor = getTableSubThemeColor;
199
+ /**
200
+ * 获取线条元素路径字符串
201
+ * @param element 线条元素
202
+ */
203
+ const getLineElementPath = (element) => {
204
+ const start = element.start.join(",");
205
+ const end = element.end.join(",");
206
+ if (element.broken) {
207
+ const mid = element.broken.join(",");
208
+ return `M${start} L${mid} L${end}`;
209
+ }
210
+ else if (element.broken2) {
211
+ const { minX, maxX, minY, maxY } = (0, exports.getElementRange)(element);
212
+ if (maxX - minX >= maxY - minY)
213
+ return `M${start} L${element.broken2[0]},${element.start[1]} L${element.broken2[0]},${element.end[1]} ${end}`;
214
+ return `M${start} L${element.start[0]},${element.broken2[1]} L${element.end[0]},${element.broken2[1]} ${end}`;
215
+ }
216
+ else if (element.curve) {
217
+ const mid = element.curve.join(",");
218
+ return `M${start} Q${mid} ${end}`;
219
+ }
220
+ else if (element.cubic) {
221
+ const [c1, c2] = element.cubic;
222
+ const p1 = c1.join(",");
223
+ const p2 = c2.join(",");
224
+ return `M${start} C${p1} ${p2} ${end}`;
225
+ }
226
+ return `M${start} L${end}`;
227
+ };
228
+ exports.getLineElementPath = getLineElementPath;
229
+ /**
230
+ * 判断一个元素是否在可视范围内
231
+ * @param element 元素
232
+ * @param parent 父元素
233
+ */
234
+ const isElementInViewport = (element, parent) => {
235
+ const elementRect = element.getBoundingClientRect();
236
+ const parentRect = parent.getBoundingClientRect();
237
+ return (elementRect.top >= parentRect.top && elementRect.bottom <= parentRect.bottom);
238
+ };
239
+ exports.isElementInViewport = isElementInViewport;
@@ -0,0 +1,3 @@
1
+ import type { HTMLNode, AST } from "./types";
2
+ export declare const splitHead: (str: string, sep: string) => string[];
3
+ export declare const format: (nodes: HTMLNode[]) => AST[];
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.format = exports.splitHead = void 0;
4
+ const splitHead = (str, sep) => {
5
+ const idx = str.indexOf(sep);
6
+ if (idx === -1)
7
+ return [str];
8
+ return [str.slice(0, idx), str.slice(idx + sep.length)];
9
+ };
10
+ exports.splitHead = splitHead;
11
+ const unquote = (str) => {
12
+ const car = str.charAt(0);
13
+ const end = str.length - 1;
14
+ const isQuoteStart = car === '"' || car === "'";
15
+ if (isQuoteStart && car === str.charAt(end)) {
16
+ return str.slice(1, end);
17
+ }
18
+ return str;
19
+ };
20
+ const formatAttributes = (attributes) => {
21
+ return attributes.map((attribute) => {
22
+ const parts = (0, exports.splitHead)(attribute.trim(), "=");
23
+ const key = parts[0];
24
+ const value = typeof parts[1] === "string" ? unquote(parts[1]) : null;
25
+ return { key, value };
26
+ });
27
+ };
28
+ const format = (nodes) => {
29
+ return nodes.map((node) => {
30
+ if (node.type === "element") {
31
+ const children = (0, exports.format)(node.children);
32
+ const item = {
33
+ type: "element",
34
+ tagName: node.tagName.toLowerCase(),
35
+ attributes: formatAttributes(node.attributes),
36
+ children,
37
+ };
38
+ return item;
39
+ }
40
+ const item = {
41
+ type: node.type,
42
+ content: node.content,
43
+ };
44
+ return item;
45
+ });
46
+ };
47
+ exports.format = format;
@@ -0,0 +1,4 @@
1
+ import { toHTML } from "./stringify";
2
+ export type { AST } from "./types";
3
+ export declare const toAST: (str: string) => import("./types").AST[];
4
+ export { toHTML };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // 参考:https://github.com/andrejewski/himalaya 用TypeScript重写并简化部分功能
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.toHTML = exports.toAST = void 0;
5
+ const lexer_1 = require("./lexer");
6
+ const parser_1 = require("./parser");
7
+ const format_1 = require("./format");
8
+ const stringify_1 = require("./stringify");
9
+ Object.defineProperty(exports, "toHTML", { enumerable: true, get: function () { return stringify_1.toHTML; } });
10
+ const toAST = (str) => {
11
+ const tokens = (0, lexer_1.lexer)(str);
12
+ const nodes = (0, parser_1.parser)(tokens);
13
+ return (0, format_1.format)(nodes);
14
+ };
15
+ exports.toAST = toAST;
@@ -0,0 +1,2 @@
1
+ import type { Token } from "./types";
2
+ export declare const lexer: (str: string) => Token[];
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.lexer = void 0;
4
+ const lodash_1 = require("lodash");
5
+ const tags_1 = require("./tags");
6
+ const jumpPosition = (state, end) => {
7
+ const len = end - state.position;
8
+ movePositopn(state, len);
9
+ };
10
+ const movePositopn = (state, len) => {
11
+ state.position = state.position + len;
12
+ };
13
+ const findTextEnd = (str, index) => {
14
+ const isEnd = false;
15
+ while (!isEnd) {
16
+ const textEnd = str.indexOf("<", index);
17
+ if (textEnd === -1) {
18
+ return textEnd;
19
+ }
20
+ const char = str.charAt(textEnd + 1);
21
+ if (char === "/" || char === "!" || /[A-Za-z0-9]/.test(char)) {
22
+ return textEnd;
23
+ }
24
+ index = textEnd + 1;
25
+ }
26
+ return -1;
27
+ };
28
+ const lexText = (state) => {
29
+ const { str } = state;
30
+ let textEnd = findTextEnd(str, state.position);
31
+ if (textEnd === state.position)
32
+ return;
33
+ if (textEnd === -1) {
34
+ textEnd = str.length;
35
+ }
36
+ const content = str.slice(state.position, textEnd);
37
+ jumpPosition(state, textEnd);
38
+ state.tokens.push({
39
+ type: "text",
40
+ content,
41
+ });
42
+ };
43
+ const lexComment = (state) => {
44
+ const { str } = state;
45
+ movePositopn(state, 4);
46
+ let contentEnd = str.indexOf("-->", state.position);
47
+ let commentEnd = contentEnd + 3;
48
+ if (contentEnd === -1) {
49
+ contentEnd = commentEnd = str.length;
50
+ }
51
+ const content = str.slice(state.position, contentEnd);
52
+ jumpPosition(state, commentEnd);
53
+ state.tokens.push({
54
+ type: "comment",
55
+ content,
56
+ });
57
+ };
58
+ const lexTagName = (state) => {
59
+ const { str } = state;
60
+ const len = str.length;
61
+ let start = state.position;
62
+ while (start < len) {
63
+ const char = str.charAt(start);
64
+ const isTagChar = !(/\s/.test(char) || char === "/" || char === ">");
65
+ if (isTagChar)
66
+ break;
67
+ start++;
68
+ }
69
+ let end = start + 1;
70
+ while (end < len) {
71
+ const char = str.charAt(end);
72
+ const isTagChar = !(/\s/.test(char) || char === "/" || char === ">");
73
+ if (!isTagChar)
74
+ break;
75
+ end++;
76
+ }
77
+ jumpPosition(state, end);
78
+ const tagName = str.slice(start, end);
79
+ state.tokens.push({
80
+ type: "tag",
81
+ content: tagName,
82
+ });
83
+ return tagName;
84
+ };
85
+ const lexTagAttributes = (state) => {
86
+ const { str, tokens } = state;
87
+ let cursor = state.position;
88
+ let quote = null;
89
+ let wordBegin = cursor;
90
+ const words = [];
91
+ const len = str.length;
92
+ while (cursor < len) {
93
+ const char = str.charAt(cursor);
94
+ if (quote) {
95
+ const isQuoteEnd = char === quote;
96
+ if (isQuoteEnd)
97
+ quote = null;
98
+ cursor++;
99
+ continue;
100
+ }
101
+ const isTagEnd = char === "/" || char === ">";
102
+ if (isTagEnd) {
103
+ if (cursor !== wordBegin)
104
+ words.push(str.slice(wordBegin, cursor));
105
+ break;
106
+ }
107
+ const isWordEnd = /\s/.test(char);
108
+ if (isWordEnd) {
109
+ if (cursor !== wordBegin)
110
+ words.push(str.slice(wordBegin, cursor));
111
+ wordBegin = cursor + 1;
112
+ cursor++;
113
+ continue;
114
+ }
115
+ const isQuoteStart = char === "'" || char === '"';
116
+ if (isQuoteStart) {
117
+ quote = char;
118
+ cursor++;
119
+ continue;
120
+ }
121
+ cursor++;
122
+ }
123
+ jumpPosition(state, cursor);
124
+ const type = "attribute";
125
+ for (let i = 0; i < words.length; i++) {
126
+ const word = words[i];
127
+ const isNotPair = word.indexOf("=") === -1;
128
+ if (isNotPair) {
129
+ const secondWord = words[i + 1];
130
+ if (secondWord && (0, lodash_1.startsWith)(secondWord, "=")) {
131
+ if (secondWord.length > 1) {
132
+ const newWord = word + secondWord;
133
+ tokens.push({ type, content: newWord });
134
+ i += 1;
135
+ continue;
136
+ }
137
+ const thirdWord = words[i + 2];
138
+ i += 1;
139
+ if (thirdWord) {
140
+ const newWord = word + "=" + thirdWord;
141
+ tokens.push({ type, content: newWord });
142
+ i += 1;
143
+ continue;
144
+ }
145
+ }
146
+ }
147
+ if ((0, lodash_1.endsWith)(word, "=")) {
148
+ const secondWord = words[i + 1];
149
+ if (secondWord && secondWord.indexOf("=") === -1) {
150
+ const newWord = word + secondWord;
151
+ tokens.push({ type, content: newWord });
152
+ i += 1;
153
+ continue;
154
+ }
155
+ const newWord = word.slice(0, -1);
156
+ tokens.push({ type, content: newWord });
157
+ continue;
158
+ }
159
+ tokens.push({ type, content: word });
160
+ }
161
+ };
162
+ const lexSkipTag = (tagName, state) => {
163
+ const { str, tokens } = state;
164
+ const safeTagName = tagName.toLowerCase();
165
+ const len = str.length;
166
+ let index = state.position;
167
+ while (index < len) {
168
+ const nextTag = str.indexOf("</", index);
169
+ if (nextTag === -1) {
170
+ lexText(state);
171
+ break;
172
+ }
173
+ const tagState = {
174
+ str,
175
+ position: state.position,
176
+ tokens: [],
177
+ };
178
+ jumpPosition(tagState, nextTag);
179
+ const name = lexTag(tagState);
180
+ if (safeTagName !== name.toLowerCase()) {
181
+ index = tagState.position;
182
+ continue;
183
+ }
184
+ if (nextTag !== state.position) {
185
+ const textStart = state.position;
186
+ jumpPosition(state, nextTag);
187
+ tokens.push({
188
+ type: "text",
189
+ content: str.slice(textStart, nextTag),
190
+ });
191
+ }
192
+ tokens.push(...tagState.tokens);
193
+ jumpPosition(state, tagState.position);
194
+ break;
195
+ }
196
+ };
197
+ const lexTag = (state) => {
198
+ const { str } = state;
199
+ const secondChar = str.charAt(state.position + 1);
200
+ const tagStartClose = secondChar === "/";
201
+ movePositopn(state, tagStartClose ? 2 : 1);
202
+ state.tokens.push({
203
+ type: "tag-start",
204
+ close: tagStartClose,
205
+ });
206
+ const tagName = lexTagName(state);
207
+ lexTagAttributes(state);
208
+ const firstChar = str.charAt(state.position);
209
+ const tagEndClose = firstChar === "/";
210
+ movePositopn(state, tagEndClose ? 2 : 1);
211
+ state.tokens.push({
212
+ type: "tag-end",
213
+ close: tagEndClose,
214
+ });
215
+ return tagName;
216
+ };
217
+ const lex = (state) => {
218
+ const str = state.str;
219
+ const len = str.length;
220
+ while (state.position < len) {
221
+ const start = state.position;
222
+ lexText(state);
223
+ if (state.position === start) {
224
+ const isComment = (0, lodash_1.startsWith)(str, "!--", start + 1);
225
+ if (isComment)
226
+ lexComment(state);
227
+ else {
228
+ const tagName = lexTag(state);
229
+ const safeTag = tagName.toLowerCase();
230
+ if (tags_1.childlessTags.includes(safeTag))
231
+ lexSkipTag(tagName, state);
232
+ }
233
+ }
234
+ }
235
+ };
236
+ const lexer = (str) => {
237
+ const state = {
238
+ str,
239
+ position: 0,
240
+ tokens: [],
241
+ };
242
+ lex(state);
243
+ return state.tokens;
244
+ };
245
+ exports.lexer = lexer;
@@ -0,0 +1,15 @@
1
+ import type { Token, HTMLNode } from "./types";
2
+ interface StackItem {
3
+ tagName: string | null;
4
+ children: HTMLNode[];
5
+ }
6
+ interface State {
7
+ stack: StackItem[];
8
+ cursor: number;
9
+ tokens: Token[];
10
+ }
11
+ export declare const parser: (tokens: Token[]) => HTMLNode[];
12
+ export declare const hasTerminalParent: (tagName: string, stack: StackItem[]) => boolean;
13
+ export declare const rewindStack: (stack: StackItem[], newLength: number) => void;
14
+ export declare const parse: (state: State) => void;
15
+ export {};