@ac-base/ac-demo-content 0.0.0-stage → 0.1.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.
@@ -0,0 +1,80 @@
1
+ const COMMENT = /<!--[\s\S]*?(?:-->|$)/y;
2
+ const TAG_OPEN = /<(\/?)([A-Za-z][\w:-]*)/y;
3
+ const TAG_CLOSE = /\/?>/y;
4
+ const ATTR_NAME = /[^\s"'<>/=]+/y;
5
+ const ATTR_VALUE = /"[^"]*"?|'[^']*'?/y;
6
+ const WHITESPACE = /\s+/y;
7
+ const TEXT = /[^<]+/y;
8
+
9
+ function match(pattern, source, index) {
10
+ pattern.lastIndex = index;
11
+ return pattern.exec(source);
12
+ }
13
+
14
+ /**
15
+ * Tokeniza HTML para resaltarlo en la vista de código. No es un parser: solo
16
+ * distingue comentarios, etiquetas, atributos, valores y texto — lo justo para
17
+ * colorear el HTML de un caso de demo. Tipos: comment, punct, tag, attr,
18
+ * string, text, plain.
19
+ */
20
+ export function tokenizeHtml(source) {
21
+ const tokens = [];
22
+ const push = (type, text) => {
23
+ if (text) tokens.push({ type, text });
24
+ };
25
+ let index = 0;
26
+ let inTag = false;
27
+
28
+ while (index < source.length) {
29
+ let found;
30
+
31
+ if (inTag) {
32
+ if ((found = match(WHITESPACE, source, index))) {
33
+ push('plain', found[0]);
34
+ } else if ((found = match(TAG_CLOSE, source, index))) {
35
+ push('punct', found[0]);
36
+ inTag = false;
37
+ } else if (source[index] === '=') {
38
+ found = ['='];
39
+ push('punct', '=');
40
+ } else if ((found = match(ATTR_VALUE, source, index))) {
41
+ push('string', found[0]);
42
+ } else if ((found = match(ATTR_NAME, source, index))) {
43
+ push('attr', found[0]);
44
+ } else {
45
+ found = [source[index]];
46
+ push('plain', found[0]);
47
+ }
48
+ } else if ((found = match(COMMENT, source, index))) {
49
+ push('comment', found[0]);
50
+ } else if ((found = match(TAG_OPEN, source, index))) {
51
+ push('punct', `<${found[1]}`);
52
+ push('tag', found[2]);
53
+ inTag = true;
54
+ } else if ((found = match(TEXT, source, index))) {
55
+ push('text', found[0]);
56
+ } else {
57
+ found = [source[index]];
58
+ push('text', found[0]);
59
+ }
60
+
61
+ index += found[0].length;
62
+ }
63
+
64
+ return tokens;
65
+ }
66
+
67
+ /** Reparte los tokens en líneas (un token multilínea, como un comentario, se parte en varios). */
68
+ export function splitTokensIntoLines(tokens) {
69
+ const lines = [[]];
70
+
71
+ for (const token of tokens) {
72
+ const parts = token.text.split('\n');
73
+ parts.forEach((part, partIndex) => {
74
+ if (partIndex > 0) lines.push([]);
75
+ if (part) lines[lines.length - 1].push({ type: token.type, text: part });
76
+ });
77
+ }
78
+
79
+ return lines;
80
+ }