import {
asMappedString,
mappedConcat,
MappedString,
mappedSubstring,
} from "./lib/mapped-text.ts";
export function executeInlineCodeHandler(
language: string,
exec: (expr: string) => string | undefined,
) {
const exprPattern = new RegExp(
"(^|[^`])`{" + language + "}[ \t]([^`]+)`",
"g",
);
return (code: string) => {
return code.replaceAll(exprPattern, (match, prefix, expr) => {
const result = exec(expr.trim());
if (result) {
return `${prefix}${result}`;
} else {
return match;
}
});
};
}
export function executeInlineCodeHandlerMapped(
language: string,
exec: (expr: string) => string | undefined,
) {
const exprPattern = new RegExp(
"(^|[^`])`{" + language + "}[ \t]([^`]+)`",
"g",
);
return (code: MappedString) => {
let matches: RegExpExecArray | null;
const result: MappedString[] = [];
let prevIndex = 0;
while ((matches = exprPattern.exec(code.value))) {
result.push(mappedSubstring(code, prevIndex, matches.index));
const matchMapped = mappedSubstring(
code,
matches.index,
matches.index + matches[0].length,
);
const prefixMapped = mappedSubstring(
code,
matches.index,
matches.index + matches[1].length,
);
const exprStr = matches[2];
const exprResult = exec(exprStr.trim());
if (exprResult) {
result.push(prefixMapped);
result.push(asMappedString(exprResult));
} else {
result.push(matchMapped);
}
prevIndex = matches.index + matches[0].length;
}
result.push(mappedSubstring(code, prevIndex));
return mappedConcat(result);
};
}