This commit is contained in:
Zef Hemel
2024-01-25 11:42:36 +01:00
parent 232a0d8df8
commit 8404256ccb
22 changed files with 188 additions and 91 deletions
+24
View File
@@ -0,0 +1,24 @@
import { assertEquals } from "../test_deps.ts";
import { parseCommand } from "./command.ts";
Deno.test("Command parser", () => {
assertEquals(parseCommand("Hello world"), { name: "Hello world", args: [] });
assertEquals(parseCommand("{[Hello world]}"), {
name: "Hello world",
args: [],
});
assertEquals(parseCommand("{[Hello world|sup]}"), {
name: "Hello world",
alias: "sup",
args: [],
});
assertEquals(parseCommand("{[Hello world](1, 2, 3)}"), {
name: "Hello world",
args: [1, 2, 3],
});
assertEquals(parseCommand("{[Hello world|sup](1, 2, 3)}"), {
name: "Hello world",
alias: "sup",
args: [1, 2, 3],
});
});
+23
View File
@@ -0,0 +1,23 @@
export const commandLinkRegex =
/^\{\[([^\]\|]+)(\|([^\]]+))?\](\(([^\)]+)\))?\}/;
export type ParsedCommand = {
name: string;
args: any[];
alias?: string;
};
export function parseCommand(command: string): ParsedCommand {
const parsedCommand: ParsedCommand = { name: command, args: [] };
const commandMatch = commandLinkRegex.exec(command);
if (commandMatch) {
parsedCommand.name = commandMatch[1];
if (commandMatch[3]) {
parsedCommand.alias = commandMatch[3];
}
parsedCommand.args = commandMatch[5]
? JSON.parse(`[${commandMatch[5]}]`)
: [];
}
return parsedCommand;
}
+1 -4
View File
@@ -1,3 +1,4 @@
import { commandLinkRegex } from "../command.ts";
import {
BlockContext,
LeafBlock,
@@ -13,7 +14,6 @@ import {
yamlLanguage,
} from "../deps.ts";
import * as ct from "./customtags.ts";
import { HashtagTag, TaskDeadlineTag } from "./customtags.ts";
import { NakedURLTag } from "./customtags.ts";
import { TaskList } from "./extended_task.ts";
@@ -65,9 +65,6 @@ const WikiLink: MarkdownConfig = {
],
};
export const commandLinkRegex =
/^\{\[([^\]\|]+)(\|([^\]]+))?\](\(([^\)]+)\))?\}/;
const CommandLink: MarkdownConfig = {
defineNodes: [
{ name: "CommandLink", style: { "CommandLink/...": ct.CommandLinkTag } },
+25 -5
View File
@@ -48,13 +48,35 @@ export function parseYamlSettings(settingsMarkdown: string): {
}
}
export const defaultSettings: BuiltinSettings = {
indexPage: "index",
hideSyncButton: false,
actionButtons: [
{
icon: "Home",
description: "Go to the index page",
command: "Navigate: Home",
},
{
icon: "Book",
description: `Open page`,
command: "Navigate: Page Picker",
},
{
icon: "Terminal",
description: `Run command`,
command: "Open Command Palette",
},
],
};
/**
* Ensures that the settings and index page exist in the given space.
* If they don't exist, default settings and index page will be created.
* @param space - The SpacePrimitives object representing the space.
* @returns A promise that resolves to the built-in settings.
*/
export async function ensureSettingsAndIndex(
export async function ensureAndLoadSettingsAndIndex(
space: SpacePrimitives,
): Promise<BuiltinSettings> {
let settingsText: string | undefined;
@@ -73,9 +95,7 @@ export async function ensureSettingsAndIndex(
} else {
console.error("Error reading settings", e.message);
console.warn("Falling back to default settings");
return {
indexPage: "index",
};
return defaultSettings;
}
settingsText = SETTINGS_TEMPLATE;
// Ok, then let's also check the index page
@@ -95,5 +115,5 @@ export async function ensureSettingsAndIndex(
const settings: any = parseYamlSettings(settingsText);
expandPropertyNames(settings);
return settings;
return { ...defaultSettings, ...settings };
}