Skip to content

Commit e9adf6c

Browse files
committed
codeblock: add backtick code fence notation for creating a code block with specific language
1 parent 703fd2c commit e9adf6c

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

lib/com/codeblock.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ export let defaultExecutor: CodeExecutor = {
3838
export class CodeBlock {
3939
code: string;
4040
language: string;
41+
detectLanguage: boolean;
4142

42-
constructor() {
43+
constructor(language?: string) {
4344
this.code = "";
4445
this.language = "";
46+
this.detectLanguage = true;
47+
48+
if (language) {
49+
this.language = language;
50+
this.detectLanguage = false;
51+
}
4552
}
4653

4754
childrenView() {
@@ -70,17 +77,17 @@ export class CodeBlock {
7077

7178
static initialize(workbench: Workbench) {
7279
workbench.commands.registerCommand({
73-
id: "make-code-snippet",
74-
title: "Make Code Snippet",
80+
id: "make-code-block",
81+
title: "Make Code Block",
7582
when: (ctx: Context) => {
7683
if (!ctx.node) return false;
7784
if (ctx.node.raw.Rel === "Fields") return false;
7885
if (ctx.node.parent && ctx.node.parent.hasComponent(Document))
7986
return false;
8087
return true;
8188
},
82-
action: (ctx: Context) => {
83-
const com = new CodeBlock();
89+
action: (ctx: Context, language?: string) => {
90+
const com = new CodeBlock(language);
8491
if (ctx?.node) {
8592
ctx.node.addComponent(com);
8693
ctx.node.changed();
@@ -110,9 +117,12 @@ const CodeEditor = {
110117
editor.textContent = editor.textContent;
111118
//@ts-ignore
112119
window.hljs.highlightBlock(editor);
113-
snippet.language =
120+
121+
if (snippet.detectLanguage) {
114122
//@ts-ignore
115-
window.hljs.highlightAuto(editor.textContent).language || "";
123+
snippet.language = window.hljs.highlightAuto(editor.textContent).language || "";
124+
}
125+
116126
});
117127
dom.jarEditor.updateCode(snippet.code);
118128
dom.jarEditor.onUpdate((code) => {

lib/ui/outline.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { Workbench, Path } from "../workbench/mod.ts";
33
import { objectCall, componentsWith, objectHas } from "../model/hooks.ts";
44
import { NodeEditor } from "./node/editor.tsx";
55

6+
// bunch of components I wish weren't direct dependencies
67
import { Checkbox } from "../com/checkbox.tsx";
78
import { Document } from "../com/document.tsx";
8-
import { getNodeView } from "../view/views.ts";
9-
109
import { Tag } from "../com/tag.tsx";
10+
import { CodeBlock } from "../com/codeblock.tsx";
11+
12+
import { getNodeView } from "../view/views.ts";
1113

1214
export interface Attrs {
1315
path: Path;
@@ -149,6 +151,18 @@ export const OutlineNode: m.Component<Attrs, State> = {
149151
case "Enter":
150152
e.preventDefault();
151153
if (e.ctrlKey || e.shiftKey || e.metaKey || e.altKey) return;
154+
155+
// first check if node should become a code block
156+
// todo: this should be a hook or some loose coupled system
157+
if (e.target.value.startsWith("```") && !node.hasComponent(CodeBlock)) {
158+
const lang = e.target.value.slice(3);
159+
if (lang) {
160+
workbench.executeCommand("make-code-block", {node, path}, lang);
161+
e.stopPropagation();
162+
return;
163+
}
164+
}
165+
152166
// cursor at end of text
153167
if (e.target.selectionStart === e.target.value.length) {
154168
if (node.childCount > 0 && workbench.workspace.getExpanded(path.head, node)) {

0 commit comments

Comments
 (0)