2023-12-27 12:38:38 +00:00
|
|
|
import { Decoration, EditorState, syntaxTree } from "../deps.ts";
|
2023-07-14 14:56:20 +00:00
|
|
|
import type { Client } from "../client.ts";
|
2022-12-22 15:20:05 +00:00
|
|
|
import {
|
|
|
|
decoratorStateField,
|
|
|
|
invisibleDecoration,
|
|
|
|
isCursorInRange,
|
2024-01-02 14:26:36 +00:00
|
|
|
shouldRenderAsCode,
|
2022-12-22 15:20:05 +00:00
|
|
|
} from "./util.ts";
|
2023-12-27 12:38:38 +00:00
|
|
|
import { MarkdownWidget } from "./markdown_widget.ts";
|
|
|
|
import { IFrameWidget } from "./iframe_widget.ts";
|
2024-01-08 16:08:26 +00:00
|
|
|
import { isTemplate } from "$sb/lib/cheap_yaml.ts";
|
2022-12-22 15:20:05 +00:00
|
|
|
|
2023-07-14 14:56:20 +00:00
|
|
|
export function fencedCodePlugin(editor: Client) {
|
2022-12-22 15:20:05 +00:00
|
|
|
return decoratorStateField((state: EditorState) => {
|
|
|
|
const widgets: any[] = [];
|
|
|
|
syntaxTree(state).iterate({
|
|
|
|
enter({ from, to, name, node }) {
|
|
|
|
if (name === "FencedCode") {
|
2024-01-02 14:26:36 +00:00
|
|
|
if (shouldRenderAsCode(state, [from, to])) {
|
|
|
|
// Don't render the widget if the cursor is inside the fenced code
|
|
|
|
return;
|
|
|
|
}
|
2022-12-22 15:20:05 +00:00
|
|
|
const text = state.sliceDoc(from, to);
|
|
|
|
const [_, lang] = text.match(/^```(\w+)?/)!;
|
2023-07-14 11:44:30 +00:00
|
|
|
const codeWidgetCallback = editor.system.codeWidgetHook
|
|
|
|
.codeWidgetCallbacks
|
2022-12-22 15:20:05 +00:00
|
|
|
.get(lang);
|
2023-12-27 12:38:38 +00:00
|
|
|
const renderMode = editor.system.codeWidgetHook.codeWidgetModes.get(
|
|
|
|
lang,
|
|
|
|
);
|
2024-01-08 16:08:26 +00:00
|
|
|
// Only custom render when we have a custom renderer, and the current page is not a template
|
|
|
|
if (codeWidgetCallback && !isTemplate(state.sliceDoc(0, from))) {
|
2022-12-22 15:20:05 +00:00
|
|
|
// We got a custom renderer!
|
|
|
|
const lineStrings = text.split("\n");
|
|
|
|
|
|
|
|
const lines: { from: number; to: number }[] = [];
|
|
|
|
let fromIt = from;
|
|
|
|
for (const line of lineStrings) {
|
|
|
|
lines.push({
|
|
|
|
from: fromIt,
|
|
|
|
to: fromIt + line.length,
|
|
|
|
});
|
|
|
|
fromIt += line.length + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const firstLine = lines[0], lastLine = lines[lines.length - 1];
|
|
|
|
|
|
|
|
// In case of doubt, back out
|
|
|
|
if (!firstLine || !lastLine) return;
|
|
|
|
|
|
|
|
widgets.push(
|
|
|
|
invisibleDecoration.range(firstLine.from, firstLine.to),
|
|
|
|
);
|
|
|
|
widgets.push(
|
|
|
|
invisibleDecoration.range(lastLine.from, lastLine.to),
|
|
|
|
);
|
|
|
|
widgets.push(
|
|
|
|
Decoration.line({
|
|
|
|
class: "sb-fenced-code-iframe",
|
|
|
|
}).range(firstLine.from),
|
|
|
|
);
|
|
|
|
widgets.push(
|
|
|
|
Decoration.line({
|
|
|
|
class: "sb-fenced-code-hide",
|
|
|
|
}).range(lastLine.from),
|
|
|
|
);
|
|
|
|
|
|
|
|
lines.slice(1, lines.length - 1).forEach((line) => {
|
|
|
|
widgets.push(
|
|
|
|
Decoration.line({ class: "sb-line-table-outside" }).range(
|
|
|
|
line.from,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-01-04 19:08:12 +00:00
|
|
|
const bodyText = lineStrings.slice(1, lineStrings.length - 1).join(
|
|
|
|
"\n",
|
|
|
|
);
|
2023-12-27 12:38:38 +00:00
|
|
|
const widget = renderMode === "markdown"
|
|
|
|
? new MarkdownWidget(
|
|
|
|
from + lineStrings[0].length + 1,
|
|
|
|
editor,
|
2024-01-04 19:08:12 +00:00
|
|
|
`widget:${editor.currentPage}:${bodyText}`,
|
|
|
|
bodyText,
|
2023-12-27 12:38:38 +00:00
|
|
|
codeWidgetCallback,
|
2023-12-27 17:05:47 +00:00
|
|
|
"sb-markdown-widget",
|
2023-12-27 12:38:38 +00:00
|
|
|
)
|
|
|
|
: new IFrameWidget(
|
|
|
|
from + lineStrings[0].length + 1,
|
|
|
|
to - lineStrings[lineStrings.length - 1].length - 1,
|
|
|
|
editor,
|
|
|
|
lineStrings.slice(1, lineStrings.length - 1).join("\n"),
|
|
|
|
codeWidgetCallback,
|
|
|
|
);
|
2022-12-22 15:20:05 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
2023-12-27 12:38:38 +00:00
|
|
|
widget: widget,
|
2022-12-22 15:20:05 +00:00
|
|
|
}).range(from),
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
name === "CodeMark"
|
|
|
|
) {
|
|
|
|
const parent = node.parent!;
|
|
|
|
// Hide ONLY if CodeMark is not insine backticks (InlineCode) and the cursor is placed outside
|
|
|
|
if (
|
|
|
|
parent.node.name !== "InlineCode" &&
|
|
|
|
!isCursorInRange(state, [parent.from, parent.to])
|
|
|
|
) {
|
|
|
|
widgets.push(
|
|
|
|
Decoration.line({
|
|
|
|
class: "sb-line-code-outside",
|
2022-12-29 11:45:53 +00:00
|
|
|
}).range(state.doc.lineAt(from).from),
|
2022-12-22 15:20:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return Decoration.set(widgets, true);
|
|
|
|
});
|
|
|
|
}
|