2022-04-29 11:37:31 +00:00
|
|
|
import {
|
|
|
|
addParentPointers,
|
|
|
|
collectNodesMatching,
|
|
|
|
ParseTree,
|
|
|
|
renderToText,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "../../common/tree.ts";
|
2022-04-20 08:56:43 +00:00
|
|
|
|
2022-04-13 12:46:52 +00:00
|
|
|
export const queryRegex =
|
2022-06-08 13:58:43 +00:00
|
|
|
/(<!--\s*#query\s+(.+?)-->)(.+?)(<!--\s*\/query\s*-->)/gs;
|
2022-04-13 12:46:52 +00:00
|
|
|
|
2022-08-09 13:37:47 +00:00
|
|
|
export const directiveStartRegex = /<!--\s*#([\w\-]+)\s+(.+?)-->/s;
|
2022-04-20 08:56:43 +00:00
|
|
|
|
2022-08-09 13:37:47 +00:00
|
|
|
export const directiveEndRegex = /<!--\s*\/([\w\-]+)\s*-->/s;
|
2022-04-20 08:56:43 +00:00
|
|
|
|
|
|
|
export function removeQueries(pt: ParseTree) {
|
|
|
|
addParentPointers(pt);
|
|
|
|
collectNodesMatching(pt, (t) => {
|
|
|
|
if (t.type !== "CommentBlock") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let text = t.children![0].text!;
|
2022-08-09 13:37:47 +00:00
|
|
|
let match = directiveStartRegex.exec(text);
|
|
|
|
if (!match) {
|
2022-04-20 08:56:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-09 13:37:47 +00:00
|
|
|
let directiveType = match[1];
|
2022-04-20 08:56:43 +00:00
|
|
|
let parentChildren = t.parent!.children!;
|
|
|
|
let index = parentChildren.indexOf(t);
|
|
|
|
let nodesToReplace: ParseTree[] = [];
|
|
|
|
for (let i = index + 1; i < parentChildren.length; i++) {
|
|
|
|
let n = parentChildren[i];
|
|
|
|
if (n.type === "CommentBlock") {
|
|
|
|
let text = n.children![0].text!;
|
2022-08-09 13:37:47 +00:00
|
|
|
let match = directiveEndRegex.exec(text);
|
|
|
|
if (match && match[1] === directiveType) {
|
2022-04-20 08:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodesToReplace.push(n);
|
|
|
|
}
|
|
|
|
let renderedText = nodesToReplace.map(renderToText).join("");
|
|
|
|
parentChildren.splice(index + 1, nodesToReplace.length, {
|
|
|
|
text: new Array(renderedText.length + 1).join(" "),
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
});
|
2022-04-13 12:46:52 +00:00
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
|
2022-04-29 11:37:31 +00:00
|
|
|
const maxWidth = 70;
|
2022-04-21 09:46:33 +00:00
|
|
|
// Nicely format an array of JSON objects as a Markdown table
|
|
|
|
export function jsonToMDTable(
|
|
|
|
jsonArray: any[],
|
2022-10-12 09:47:13 +00:00
|
|
|
valueTransformer: (k: string, v: any) => string = (k, v) => "" + v,
|
2022-04-21 09:46:33 +00:00
|
|
|
): string {
|
2022-04-29 11:37:31 +00:00
|
|
|
let fieldWidths = new Map<string, number>();
|
2022-04-21 09:46:33 +00:00
|
|
|
for (let entry of jsonArray) {
|
|
|
|
for (let k of Object.keys(entry)) {
|
2022-04-29 11:37:31 +00:00
|
|
|
let fieldWidth = fieldWidths.get(k);
|
|
|
|
if (!fieldWidth) {
|
|
|
|
fieldWidth = valueTransformer(k, entry[k]).length;
|
|
|
|
} else {
|
|
|
|
fieldWidth = Math.max(valueTransformer(k, entry[k]).length, fieldWidth);
|
|
|
|
}
|
|
|
|
fieldWidths.set(k, fieldWidth);
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 11:37:31 +00:00
|
|
|
|
|
|
|
let fullWidth = 0;
|
|
|
|
for (let v of fieldWidths.values()) {
|
|
|
|
fullWidth += v + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let headerList = [...fieldWidths.keys()];
|
2022-04-21 09:46:33 +00:00
|
|
|
let lines = [];
|
2022-04-29 11:37:31 +00:00
|
|
|
lines.push(
|
|
|
|
"|" +
|
|
|
|
headerList
|
|
|
|
.map(
|
|
|
|
(headerName) =>
|
|
|
|
headerName +
|
2022-10-12 09:47:13 +00:00
|
|
|
charPad(" ", fieldWidths.get(headerName)! - headerName.length),
|
2022-04-29 11:37:31 +00:00
|
|
|
)
|
|
|
|
.join("|") +
|
2022-10-12 09:47:13 +00:00
|
|
|
"|",
|
2022-04-29 11:37:31 +00:00
|
|
|
);
|
|
|
|
lines.push(
|
|
|
|
"|" +
|
|
|
|
headerList
|
|
|
|
.map((title) => charPad("-", fieldWidths.get(title)!))
|
|
|
|
.join("|") +
|
2022-10-12 09:47:13 +00:00
|
|
|
"|",
|
2022-04-29 11:37:31 +00:00
|
|
|
);
|
2022-04-21 09:46:33 +00:00
|
|
|
for (const val of jsonArray) {
|
|
|
|
let el = [];
|
|
|
|
for (let prop of headerList) {
|
2022-04-29 11:37:31 +00:00
|
|
|
let s = valueTransformer(prop, val[prop]);
|
|
|
|
el.push(s + charPad(" ", fieldWidths.get(prop)! - s.length));
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
lines.push("|" + el.join("|") + "|");
|
|
|
|
}
|
|
|
|
return lines.join("\n");
|
2022-04-29 11:37:31 +00:00
|
|
|
|
|
|
|
function charPad(ch: string, length: number) {
|
|
|
|
if (fullWidth > maxWidth && ch === "") {
|
|
|
|
return "";
|
|
|
|
} else if (fullWidth > maxWidth && ch === "-") {
|
|
|
|
return "--";
|
|
|
|
}
|
|
|
|
if (length < 1) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return new Array(length + 1).join(ch);
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|