Fixes #539 linter support, now checking YAML formats
This commit is contained in:
@@ -174,3 +174,8 @@ functions:
|
||||
|
||||
renderMentions:
|
||||
path: "./mentions_ps.ts:renderMentions"
|
||||
|
||||
lintYAML:
|
||||
path: lint.ts:lintYAML
|
||||
events:
|
||||
- editor:lint
|
||||
@@ -0,0 +1,83 @@
|
||||
import { editor, markdown, YAML } from "$sb/syscalls.ts";
|
||||
import { LintDiagnostic } from "$sb/types.ts";
|
||||
import {
|
||||
findNodeOfType,
|
||||
renderToText,
|
||||
traverseTreeAsync,
|
||||
} from "$sb/lib/tree.ts";
|
||||
|
||||
export async function lintYAML(): Promise<LintDiagnostic[]> {
|
||||
const text = await editor.getText();
|
||||
const tree = await markdown.parseMarkdown(text);
|
||||
const diagnostics: LintDiagnostic[] = [];
|
||||
await traverseTreeAsync(tree, async (node) => {
|
||||
if (node.type === "FrontMatterCode") {
|
||||
const lintResult = await lintYaml(
|
||||
renderToText(node),
|
||||
node.from!,
|
||||
);
|
||||
if (lintResult) {
|
||||
diagnostics.push(lintResult);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (node.type === "FencedCode") {
|
||||
const codeInfo = findNodeOfType(node, "CodeInfo")!;
|
||||
if (!codeInfo) {
|
||||
return true;
|
||||
}
|
||||
const codeLang = codeInfo.children![0].text!;
|
||||
// All known YAML formats
|
||||
if (
|
||||
codeLang === "template" || codeLang === "yaml" ||
|
||||
codeLang.startsWith("#")
|
||||
) {
|
||||
const codeText = findNodeOfType(node, "CodeText");
|
||||
if (!codeText) {
|
||||
return true;
|
||||
}
|
||||
const yamlCode = renderToText(codeText);
|
||||
const lintResult = await lintYaml(
|
||||
yamlCode,
|
||||
codeText.from!,
|
||||
);
|
||||
if (lintResult) {
|
||||
diagnostics.push(lintResult);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
const errorRegex = /at line (\d+),? column (\d+)/;
|
||||
|
||||
async function lintYaml(
|
||||
yamlText: string,
|
||||
from: number,
|
||||
): Promise<LintDiagnostic | undefined> {
|
||||
try {
|
||||
await YAML.parse(yamlText);
|
||||
} catch (e) {
|
||||
const errorMatch = errorRegex.exec(e.message);
|
||||
if (errorMatch) {
|
||||
console.log("YAML error", e.message);
|
||||
const line = parseInt(errorMatch[1], 10) - 1;
|
||||
const yamlLines = yamlText.split("\n");
|
||||
let pos = from;
|
||||
for (let i = 0; i < line; i++) {
|
||||
pos += yamlLines[i].length + 1;
|
||||
}
|
||||
const endPos = pos + yamlLines[line]?.length || pos;
|
||||
|
||||
return {
|
||||
from: pos,
|
||||
to: endPos,
|
||||
severity: "error",
|
||||
message: e.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
+86
-4
@@ -1,10 +1,15 @@
|
||||
import type { IndexTreeEvent } from "$sb/app_event.ts";
|
||||
import { space } from "$sb/syscalls.ts";
|
||||
import { editor, markdown, space, YAML } from "$sb/syscalls.ts";
|
||||
|
||||
import type { PageMeta } from "$sb/types.ts";
|
||||
import type { LintDiagnostic, PageMeta } from "$sb/types.ts";
|
||||
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
|
||||
import { extractAttributes } from "$sb/lib/attribute.ts";
|
||||
import { indexObjects } from "./api.ts";
|
||||
import {
|
||||
findNodeOfType,
|
||||
renderToText,
|
||||
traverseTreeAsync,
|
||||
} from "$sb/lib/tree.ts";
|
||||
|
||||
export async function indexPage({ name, tree }: IndexTreeEvent) {
|
||||
if (name.startsWith("_")) {
|
||||
@@ -22,7 +27,84 @@ export async function indexPage({ name, tree }: IndexTreeEvent) {
|
||||
pageMeta.tags = [...new Set(["page", ...pageMeta.tags || []])];
|
||||
|
||||
// console.log("Page object", pageObj);
|
||||
|
||||
// console.log("Extracted page meta data", pageMeta);
|
||||
await indexObjects<PageMeta>(name, [pageMeta]);
|
||||
}
|
||||
|
||||
export async function lintFrontmatter(): Promise<LintDiagnostic[]> {
|
||||
const text = await editor.getText();
|
||||
const tree = await markdown.parseMarkdown(text);
|
||||
const diagnostics: LintDiagnostic[] = [];
|
||||
await traverseTreeAsync(tree, async (node) => {
|
||||
if (node.type === "FrontMatterCode") {
|
||||
const lintResult = await lintYaml(
|
||||
renderToText(node),
|
||||
node.from!,
|
||||
node.to!,
|
||||
);
|
||||
if (lintResult) {
|
||||
diagnostics.push(lintResult);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (node.type === "FencedCode") {
|
||||
const codeInfo = findNodeOfType(node, "CodeInfo")!;
|
||||
if (!codeInfo) {
|
||||
return true;
|
||||
}
|
||||
const codeLang = codeInfo.children![0].text!;
|
||||
// All known YAML formats
|
||||
if (
|
||||
codeLang === "template" || codeLang === "yaml" ||
|
||||
codeLang.startsWith("#")
|
||||
) {
|
||||
const codeText = findNodeOfType(node, "CodeText");
|
||||
if (!codeText) {
|
||||
return true;
|
||||
}
|
||||
const yamlCode = renderToText(codeText);
|
||||
const lintResult = await lintYaml(
|
||||
yamlCode,
|
||||
codeText.from!,
|
||||
codeText.to!,
|
||||
);
|
||||
if (lintResult) {
|
||||
diagnostics.push(lintResult);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
const errorRegex = /at line (\d+),? column (\d+)/;
|
||||
|
||||
async function lintYaml(
|
||||
yamlText: string,
|
||||
from: number,
|
||||
to: number,
|
||||
): Promise<LintDiagnostic | undefined> {
|
||||
try {
|
||||
await YAML.parse(yamlText);
|
||||
} catch (e) {
|
||||
const errorMatch = errorRegex.exec(e.message);
|
||||
if (errorMatch) {
|
||||
console.log("YAML error", e.message);
|
||||
// const line = parseInt(errorMatch[1], 10) - 1;
|
||||
// const yamlLines = yamlText.split("\n");
|
||||
// let pos = posOffset;
|
||||
// for (let i = 0; i < line; i++) {
|
||||
// pos += yamlLines[i].length + 1;
|
||||
// }
|
||||
// const endPos = pos + yamlLines[line].length;
|
||||
|
||||
return {
|
||||
from,
|
||||
to,
|
||||
severity: "error",
|
||||
message: e.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user