|
| 1 | +function parseBlocks(tokens, options = {}) { |
| 2 | + const result = {}; |
| 3 | + let currentBlock = null; |
| 4 | + |
| 5 | + for (const token of tokens) { |
| 6 | + if (token.type === "meta") { |
| 7 | + result.meta = result.meta || {}; |
| 8 | + result.meta[token.key] = token.value; |
| 9 | + continue; |
| 10 | + } |
| 11 | + |
| 12 | + if (token.type === "command") { |
| 13 | + currentBlock = token.value.toLowerCase(); |
| 14 | + if (!result[currentBlock]) { |
| 15 | + result[currentBlock] = |
| 16 | + currentBlock === "meeting" |
| 17 | + ? [] |
| 18 | + : currentBlock === "participants" || currentBlock === "tags" |
| 19 | + ? {} |
| 20 | + : []; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + if (!currentBlock) continue; |
| 25 | + |
| 26 | + switch (token.type) { |
| 27 | + case "declaration": |
| 28 | + if (currentBlock === "participants") { |
| 29 | + const [name, alias, email] = token.value.split(","); |
| 30 | + result[currentBlock][token.key] = { |
| 31 | + name: name?.trim(), |
| 32 | + alias: alias?.trim(), |
| 33 | + email: email?.trim(), |
| 34 | + }; |
| 35 | + } else if (currentBlock === "tags") { |
| 36 | + result[currentBlock][token.key] = token.value; |
| 37 | + } else { |
| 38 | + // General key:value map |
| 39 | + result[currentBlock][token.key] = token.value; |
| 40 | + } |
| 41 | + break; |
| 42 | + |
| 43 | + case "entry": |
| 44 | + if (currentBlock === "tasks") { |
| 45 | + const done = token.value.startsWith("[x]"); |
| 46 | + |
| 47 | + const ptp = token.raw.match(/@ptp=([^\s]+)/)?.[1] || null; |
| 48 | + const subject = token.raw.match(/=([^\s]+)/)?.[1] || null; |
| 49 | + const tag = token.raw.match(/@tag=([^\s]+)/)?.[1] || null; |
| 50 | + |
| 51 | + const cleanedText = token.value |
| 52 | + .replace(/^\[(x| )\]/, "") |
| 53 | + .replace(/@ptp=[^\s]+/g, "") |
| 54 | + .replace(/@tag=[^\s]+/g, "") |
| 55 | + .replace(/=[^\s]+/g, "") |
| 56 | + .replace(/\/\/.*/, "") // comments |
| 57 | + .trim(); |
| 58 | + |
| 59 | + result[currentBlock].push({ |
| 60 | + raw: token.raw, // raw line (für referenceLinker) |
| 61 | + text: cleanedText, // cleaned text for display |
| 62 | + done, |
| 63 | + meta: { |
| 64 | + ptp, |
| 65 | + subject, |
| 66 | + tag, |
| 67 | + }, |
| 68 | + }); |
| 69 | + } else { |
| 70 | + result[currentBlock].push(token.value); |
| 71 | + } |
| 72 | + break; |
| 73 | + |
| 74 | + case "heading": |
| 75 | + case "inlineCommand": |
| 76 | + case "text": |
| 77 | + if (currentBlock === "meeting") { |
| 78 | + result[currentBlock].push(token.raw); |
| 79 | + } |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return result; |
| 85 | +} |
| 86 | + |
| 87 | +module.exports = {parseBlocks}; |
0 commit comments