@meldscience/meld
Version:
pipeable one-shot prompt scripting toolkit
67 lines • 2.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.remarkMeldDirectives = void 0;
const parseMeldDirective_1 = require("./parseMeldDirective");
const unist_util_visit_1 = require("unist-util-visit");
const remarkMeldDirectives = function remarkMeldDirectives() {
return (tree, file) => {
// Store the AST in file.data for testing
file.data.ast = tree;
(0, unist_util_visit_1.visit)(tree, 'text', (node, index, parent) => {
if (!node.value || !parent || index === null)
return;
const newNodes = [];
let lastIndex = 0;
const regex = /@(cmd|import)(?:\[([^\[\]]*(?:\[[^\[\]]*\][^\[\]]*)*)\])?(?:\s+as\s+#{1,6}(?:\s+"[^"]*")?)?/g;
let match;
console.log('Node value:', node.value);
while ((match = regex.exec(node.value)) !== null) {
const [fullMatch, type, content] = match;
const start = match.index;
console.log('Full match:', fullMatch);
console.log('Type:', type);
console.log('Content:', content);
// Add text before the match
if (start > lastIndex) {
newNodes.push({
type: 'text',
value: node.value.slice(lastIndex, start)
});
}
try {
if (!content || !fullMatch.includes('[') || !fullMatch.includes(']')) {
throw new Error(type === 'cmd' ? 'Invalid command format' : 'Invalid import format');
}
const meldNode = (0, parseMeldDirective_1.buildMeldDirectiveNode)(fullMatch);
newNodes.push(meldNode);
}
catch (err) {
// If parsing fails, keep the original text
newNodes.push({
type: 'text',
value: fullMatch
});
newNodes.push({
type: 'text',
value: ` Error: ${err.message}`
});
}
lastIndex = start + fullMatch.length;
}
// Add remaining text
if (lastIndex < node.value.length) {
newNodes.push({
type: 'text',
value: node.value.slice(lastIndex)
});
}
if (newNodes.length > 0) {
// Replace the current node with our new nodes
parent.children.splice(index, 1, ...newNodes);
return index + newNodes.length;
}
});
};
};
exports.remarkMeldDirectives = remarkMeldDirectives;
//# sourceMappingURL=remarkMeldDirectives.js.map