Refined implementation of #560
This commit is contained in:
@@ -33,9 +33,3 @@ export type ActionButton = {
|
||||
export type EmojiConfig = {
|
||||
aliases: string[][];
|
||||
};
|
||||
|
||||
export type Decoration = {
|
||||
tag: string;
|
||||
prefix: string;
|
||||
};
|
||||
|
||||
|
||||
@@ -16,3 +16,4 @@ export * as YAML from "./syscalls/yaml.ts";
|
||||
export * as mq from "./syscalls/mq.ts";
|
||||
export * from "./syscall.ts";
|
||||
export * as datastore from "./syscalls/datastore.ts";
|
||||
export * as decoration from "./syscalls/decoration.ts";
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { PageMeta } from "$sb/types.ts";
|
||||
|
||||
export function applyDecorationsToPages(
|
||||
pages: PageMeta[],
|
||||
): Promise<PageMeta[]> {
|
||||
return syscall("decoration.applyDecorationsToPages", pages);
|
||||
}
|
||||
+11
-1
@@ -16,11 +16,21 @@ export type PageMeta = ObjectValue<
|
||||
name: string;
|
||||
created: string; // indexing it as a string
|
||||
lastModified: string; // indexing it as a string
|
||||
lastOpened?: number;
|
||||
perm: "ro" | "rw";
|
||||
lastOpened?: number;
|
||||
pageDecoration?: PageDecoration;
|
||||
} & Record<string, any>
|
||||
>;
|
||||
|
||||
/**
|
||||
* Decorates a page when it matches certain criteria
|
||||
*/
|
||||
export type PageDecoration = {
|
||||
where?: string;
|
||||
whereParsed?: QueryExpression;
|
||||
prefix: string;
|
||||
};
|
||||
|
||||
export type AttachmentMeta = ObjectValue<
|
||||
{
|
||||
name: string;
|
||||
|
||||
@@ -7,20 +7,9 @@ import {
|
||||
import { listFilesCached } from "../federation/federation.ts";
|
||||
import { queryObjects } from "../index/plug_api.ts";
|
||||
import { folderName } from "$sb/lib/resolve.ts";
|
||||
import { readSetting } from "$sb/lib/settings_page.ts";
|
||||
import { editor } from "$sb/syscalls.ts"
|
||||
import type { Decoration } from "$lib/web.ts";
|
||||
|
||||
let decorations: Decoration[] = [];
|
||||
|
||||
import { decoration } from "$sb/syscalls.ts";
|
||||
// Completion
|
||||
export async function pageComplete(completeEvent: CompleteEvent) {
|
||||
try {
|
||||
await updateDecoratorConfig();
|
||||
} catch (err: any) {
|
||||
await editor.flashNotification(err.message, "error");
|
||||
}
|
||||
|
||||
// Try to match [[wikilink]]
|
||||
let isWikilink = true;
|
||||
let match = /\[\[([^\]@$#:\{}]*)$/.exec(completeEvent.linePrefix);
|
||||
@@ -89,14 +78,16 @@ export async function pageComplete(completeEvent: CompleteEvent) {
|
||||
}
|
||||
|
||||
const folder = folderName(completeEvent.pageName);
|
||||
|
||||
allPages = await decoration.applyDecorationsToPages(allPages as PageMeta[]);
|
||||
|
||||
return {
|
||||
from: completeEvent.pos - match[1].length,
|
||||
options: allPages.map((pageMeta) => {
|
||||
const completions: any[] = [];
|
||||
let namePrefix = "";
|
||||
const decor = decorations.find(d => pageMeta.tags?.some((t: any) => d.tag === t));
|
||||
if (decor) {
|
||||
namePrefix = decor.prefix;
|
||||
if ((pageMeta as PageMeta).pageDecoration?.prefix) {
|
||||
namePrefix = pageMeta.pageDecoration?.prefix;
|
||||
}
|
||||
if (isWikilink) {
|
||||
if (pageMeta.displayName) {
|
||||
@@ -157,7 +148,6 @@ export async function pageComplete(completeEvent: CompleteEvent) {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
|
||||
const name = fileMeta.name.substring(0, fileMeta.name.length - 3);
|
||||
return {
|
||||
@@ -169,17 +159,3 @@ function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
|
||||
lastModified: new Date(fileMeta.lastModified).toISOString(),
|
||||
} as PageMeta;
|
||||
}
|
||||
|
||||
let lastConfigUpdate = 0;
|
||||
|
||||
async function updateDecoratorConfig() {
|
||||
// Update at most every 5 seconds
|
||||
if (Date.now() < lastConfigUpdate + 5000) return;
|
||||
lastConfigUpdate = Date.now();
|
||||
const decoratorConfig = await readSetting("decorations");
|
||||
if (!decoratorConfig) {
|
||||
return;
|
||||
}
|
||||
|
||||
decorations = decoratorConfig;
|
||||
}
|
||||
|
||||
@@ -573,9 +573,11 @@ export function renderMarkdownToHtml(
|
||||
t.attrs!.href = options.translateUrls!(t.attrs!.href, "link");
|
||||
}
|
||||
if (t.attrs!["data-ref"]?.length) {
|
||||
const pageMeta = allPages.find(p => t.attrs!["data-ref"]!.startsWith(p.name));
|
||||
const pageMeta = allPages.find((p) =>
|
||||
t.attrs!["data-ref"]!.startsWith(p.name)
|
||||
);
|
||||
if (pageMeta) {
|
||||
t.body = [(pageMeta.pageDecorations?.prefix ?? "") + t.body]
|
||||
t.body = [(pageMeta.pageDecoration?.prefix ?? "") + t.body];
|
||||
}
|
||||
}
|
||||
if (t.body.length === 0) {
|
||||
|
||||
+2
-3
@@ -1,10 +1,9 @@
|
||||
import { Manifest } from "../lib/manifest.ts";
|
||||
import { PageMeta } from "../plug-api/types.ts";
|
||||
import { PageDecoration, PageMeta } from "../plug-api/types.ts";
|
||||
import { AppCommand } from "../lib/command.ts";
|
||||
import { defaultSettings } from "$common/settings.ts";
|
||||
import {
|
||||
ActionButton,
|
||||
Decoration,
|
||||
EmojiConfig,
|
||||
FilterOption,
|
||||
Notification,
|
||||
@@ -22,10 +21,10 @@ export type BuiltinSettings = {
|
||||
maximumAttachmentSize?: number;
|
||||
defaultLinkStyle?: string;
|
||||
actionButtons: ActionButton[];
|
||||
pageDecorations?: PageDecoration[];
|
||||
// Format: compatible with docker ignore
|
||||
spaceIgnore?: string;
|
||||
emoji?: EmojiConfig;
|
||||
decorations?: Decoration[];
|
||||
};
|
||||
|
||||
export type PanelConfig = {
|
||||
|
||||
+29
-6
@@ -713,6 +713,11 @@ export class Client {
|
||||
this.currentPage,
|
||||
meta,
|
||||
);
|
||||
|
||||
// At this all the essential stuff is done, let's proceed
|
||||
resolve();
|
||||
|
||||
// In the background we'll fetch any enriched meta data, if any
|
||||
const enrichedMeta = await this.clientSystem.getObjectByRef<
|
||||
PageMeta
|
||||
>(
|
||||
@@ -720,12 +725,12 @@ export class Client {
|
||||
"page",
|
||||
this.currentPage,
|
||||
);
|
||||
this.ui.viewDispatch({
|
||||
type: "update-current-page-meta",
|
||||
meta: enrichedMeta,
|
||||
});
|
||||
|
||||
resolve();
|
||||
if (enrichedMeta) {
|
||||
this.ui.viewDispatch({
|
||||
type: "update-current-page-meta",
|
||||
meta: enrichedMeta,
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
this.flashNotification(
|
||||
@@ -1104,6 +1109,24 @@ export class Client {
|
||||
meta: doc.meta,
|
||||
});
|
||||
|
||||
// Fetch (possibly) enriched meta data asynchronously
|
||||
await this.clientSystem.getObjectByRef<
|
||||
PageMeta
|
||||
>(
|
||||
this.currentPage,
|
||||
"page",
|
||||
this.currentPage,
|
||||
).then((enrichedMeta) => {
|
||||
if (!enrichedMeta) {
|
||||
// Nothing in the store, revert to default
|
||||
enrichedMeta = doc.meta;
|
||||
}
|
||||
this.ui.viewDispatch({
|
||||
type: "update-current-page-meta",
|
||||
meta: enrichedMeta,
|
||||
});
|
||||
}).catch(console.error);
|
||||
|
||||
const editorState = createEditorState(
|
||||
this,
|
||||
pageName,
|
||||
|
||||
@@ -43,6 +43,7 @@ import { createKeyBindings } from "./editor_state.ts";
|
||||
import { CommonSystem } from "$common/common_system.ts";
|
||||
import { DataStoreMQ } from "$lib/data/mq.datastore.ts";
|
||||
import { plugPrefix } from "$common/spaces/constants.ts";
|
||||
import { decorationSyscalls } from "./syscalls/decoration.ts";
|
||||
|
||||
const plugNameExtractRegex = /\/(.+)\.plug\.js$/;
|
||||
|
||||
@@ -168,6 +169,7 @@ export class ClientSystem extends CommonSystem {
|
||||
spaceReadSyscalls(this.client),
|
||||
systemSyscalls(this.system, false, this, this.client),
|
||||
markdownSyscalls(),
|
||||
decorationSyscalls(this.client),
|
||||
assetSyscalls(this.system),
|
||||
yamlSyscalls(),
|
||||
templateSyscalls(this.ds),
|
||||
|
||||
@@ -70,7 +70,7 @@ export function cleanWikiLinkPlugin(client: Client) {
|
||||
p.name == url
|
||||
);
|
||||
const linkText = alias ||
|
||||
(pageMeta?.pageDecorations?.prefix ?? "") +
|
||||
(pageMeta?.pageDecoration?.prefix ?? "") +
|
||||
(url.includes("/") ? url.split("/").pop()! : url);
|
||||
|
||||
// And replace it with a widget
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { FilterList } from "./filter.tsx";
|
||||
import { FilterOption, Decoration } from "$lib/web.ts";
|
||||
import { FilterOption } from "$lib/web.ts";
|
||||
import { CompletionContext, CompletionResult } from "@codemirror/autocomplete";
|
||||
import { PageMeta } from "../../plug-api/types.ts";
|
||||
import { isFederationPath } from "$sb/lib/resolve.ts";
|
||||
@@ -65,7 +65,7 @@ export function PageNavigator({
|
||||
}
|
||||
options.push({
|
||||
...pageMeta,
|
||||
name: (pageMeta.pageDecorations?.prefix ?? "") + pageMeta.name,
|
||||
name: (pageMeta.pageDecoration?.prefix ?? "") + pageMeta.name,
|
||||
description,
|
||||
orderId: orderId,
|
||||
});
|
||||
|
||||
+3
-2
@@ -78,7 +78,7 @@ export class MainUI {
|
||||
useEffect(() => {
|
||||
if (viewState.currentPage) {
|
||||
document.title =
|
||||
(viewState.currentPageMeta?.pageDecorations?.prefix ?? "") +
|
||||
(viewState.currentPageMeta?.pageDecoration?.prefix ?? "") +
|
||||
viewState.currentPage;
|
||||
}
|
||||
}, [viewState.currentPage, viewState.currentPageMeta]);
|
||||
@@ -316,7 +316,8 @@ export class MainUI {
|
||||
style={{ flex: viewState.panels.lhs.mode }}
|
||||
/>
|
||||
)}
|
||||
pageNamePrefix={viewState.currentPageMeta?.pageDecorations?.prefix ??
|
||||
pageNamePrefix={viewState.currentPageMeta?.pageDecoration
|
||||
?.prefix ??
|
||||
""}
|
||||
/>
|
||||
<div id="sb-main">
|
||||
|
||||
+13
-57
@@ -1,6 +1,6 @@
|
||||
import { Decoration } from "$lib/web.ts";
|
||||
import { PageMeta } from "../plug-api/types.ts";
|
||||
import { Action, AppViewState } from "../type/web.ts";
|
||||
import { decoratePageMeta } from "./syscalls/decoration.ts";
|
||||
|
||||
export default function reducer(
|
||||
state: AppViewState,
|
||||
@@ -22,20 +22,6 @@ export default function reducer(
|
||||
};
|
||||
case "page-loaded": {
|
||||
const mouseDetected = window.matchMedia("(pointer:fine)").matches;
|
||||
const pageMeta = state.allPages.find((p) => p.name == action.meta.name);
|
||||
const decor = state.settings.decorations?.filter((d) =>
|
||||
pageMeta?.tags?.some((t) => d.tag === t)
|
||||
);
|
||||
if (decor && decor.length > 0) {
|
||||
const mergedDecorations = decor.reduceRight((accumulator, el) => {
|
||||
accumulator = { ...accumulator, ...el };
|
||||
return accumulator;
|
||||
});
|
||||
if (mergedDecorations) {
|
||||
const { tag, ...currPageDecorations } = mergedDecorations;
|
||||
action.meta.pageDecorations = currPageDecorations;
|
||||
}
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
isLoading: false,
|
||||
@@ -61,12 +47,14 @@ export default function reducer(
|
||||
};
|
||||
}
|
||||
case "update-current-page-meta": {
|
||||
if (state.settings.decorations) {
|
||||
if (state.settings.pageDecorations) {
|
||||
decoratePageMeta(
|
||||
action.meta,
|
||||
"",
|
||||
action.meta,
|
||||
state.settings.decorations,
|
||||
state.settings.pageDecorations,
|
||||
);
|
||||
// Update in the allPages list as well
|
||||
state.allPages = state.allPages.map((pageMeta) =>
|
||||
pageMeta.name === action.meta.name ? action.meta : pageMeta
|
||||
);
|
||||
}
|
||||
return {
|
||||
@@ -89,23 +77,21 @@ export default function reducer(
|
||||
const oldPageMeta = new Map(
|
||||
[...state.allPages].map((pm) => [pm.name, pm]),
|
||||
);
|
||||
let currPageMeta = oldPageMeta.get(state.currentPage!);
|
||||
if (currPageMeta === undefined) {
|
||||
currPageMeta = {} as PageMeta;
|
||||
}
|
||||
let currPageMeta: PageMeta | undefined;
|
||||
for (const pageMeta of action.allPages) {
|
||||
const oldPageMetaItem = oldPageMeta.get(pageMeta.name);
|
||||
if (oldPageMetaItem && oldPageMetaItem.lastOpened) {
|
||||
pageMeta.lastOpened = oldPageMetaItem.lastOpened;
|
||||
}
|
||||
if (state.settings.decorations) {
|
||||
if (state.settings.pageDecorations) {
|
||||
decoratePageMeta(
|
||||
pageMeta,
|
||||
state.currentPage!,
|
||||
currPageMeta,
|
||||
state.settings.decorations,
|
||||
state.settings.pageDecorations,
|
||||
);
|
||||
}
|
||||
if (pageMeta.name === state.currentPage) {
|
||||
currPageMeta = pageMeta;
|
||||
}
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
@@ -246,33 +232,3 @@ export default function reducer(
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorates (= attaches a pageDecorations field) to the pageMeta object when a matching decorator is found
|
||||
*/
|
||||
function decoratePageMeta(
|
||||
pageMeta: PageMeta,
|
||||
currentPage: string,
|
||||
currPageMeta: PageMeta,
|
||||
decorations: Decoration[],
|
||||
) {
|
||||
const decor = decorations.filter((d) =>
|
||||
pageMeta.tags?.some((t: any) => d.tag === t)
|
||||
);
|
||||
// Page can have multiple decorations applied via different tags, accumulate them.
|
||||
// The decorations higher in the decorations list defined in SETTINGS gets
|
||||
// higher precedence.
|
||||
if (decor && decor.length > 0) {
|
||||
const mergedDecorations = decor.reduceRight((accumulator, el) => {
|
||||
accumulator = { ...accumulator, ...el };
|
||||
return accumulator;
|
||||
});
|
||||
if (mergedDecorations) {
|
||||
const { tag, ...currPageDecorations } = mergedDecorations;
|
||||
pageMeta.pageDecorations = currPageDecorations;
|
||||
if (pageMeta.name === currentPage) {
|
||||
currPageMeta!.pageDecorations = currPageDecorations;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,14 +212,9 @@ body {
|
||||
align-items: baseline;
|
||||
flex: 0 0 auto;
|
||||
text-align: left;
|
||||
padding: 1px;
|
||||
margin-right: 3px;
|
||||
padding-top: 3px;
|
||||
font-family: var(--ui-font);
|
||||
|
||||
&:empty {
|
||||
margin-right: 0;
|
||||
padding: 0;
|
||||
}
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.sb-panel {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { PageDecoration, PageMeta } from "$sb/types.ts";
|
||||
import { SysCallMapping } from "$lib/plugos/system.ts";
|
||||
import { Client } from "../client.ts";
|
||||
import { parseTreeToAST } from "$sb/lib/tree.ts";
|
||||
import { lezerToParseTree } from "$common/markdown_parser/parse_tree.ts";
|
||||
import { expressionLanguage } from "$common/template/template_parser.ts";
|
||||
import { expressionToKvQueryExpression } from "$sb/lib/parse-query.ts";
|
||||
import { evalQueryExpression } from "$sb/lib/query_expression.ts";
|
||||
import { builtinFunctions } from "$lib/builtin_query_functions.ts";
|
||||
|
||||
export function decorationSyscalls(
|
||||
client: Client,
|
||||
): SysCallMapping {
|
||||
return {
|
||||
"decoration.applyDecorationsToPages": (
|
||||
_ctx,
|
||||
pages: PageMeta[],
|
||||
): PageMeta[] => {
|
||||
if (client.settings.pageDecorations) {
|
||||
for (const pageMeta of pages) {
|
||||
decoratePageMeta(pageMeta, client.settings.pageDecorations);
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorates (= attaches a pageDecoration field) to the pageMeta object when a matching decorator is found
|
||||
*/
|
||||
export function decoratePageMeta(
|
||||
pageMeta: PageMeta,
|
||||
decorations: PageDecoration[],
|
||||
) {
|
||||
if (!pageMeta) {
|
||||
return;
|
||||
}
|
||||
for (const decoration of decorations) {
|
||||
if (!decoration.where) {
|
||||
continue;
|
||||
}
|
||||
// whereParsed is effectively a cached version of the parsed where expression
|
||||
// Let's check if it's populated
|
||||
if (!decoration.whereParsed) {
|
||||
// If not, populate it
|
||||
try {
|
||||
const ast = parseTreeToAST(lezerToParseTree(
|
||||
decoration.where,
|
||||
expressionLanguage.parser.parse(decoration.where).topNode,
|
||||
));
|
||||
decoration.whereParsed = expressionToKvQueryExpression(
|
||||
ast[1],
|
||||
);
|
||||
} catch (e: any) {
|
||||
console.error(
|
||||
"Failed to parse 'where' expression in decoration:",
|
||||
e,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (
|
||||
evalQueryExpression(
|
||||
decoration.whereParsed,
|
||||
pageMeta,
|
||||
{},
|
||||
builtinFunctions,
|
||||
)
|
||||
) {
|
||||
pageMeta.pageDecoration = decoration;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,9 @@ release.
|
||||
## Edge
|
||||
_These features are not yet properly released, you need to use [the edge builds](https://community.silverbullet.md/t/living-on-the-edge-builds/27) to try them._
|
||||
|
||||
* Nothing new since 0.8.1 yet!
|
||||
* [[Page Decorations]] are here (initial implementation by [Deepak Narayan](https://github.com/silverbulletmd/silverbullet/pull/940), later refined by Zef)
|
||||
* **Fix:** very large spaces would let the server blow up when saving snapshots. This is now fixed.
|
||||
* **Fix:** Conflict copies could no longer be edited, whoops (initial fix by [Semyon Novikov](https://github.com/silverbulletmd/silverbullet/pull/939), later refined by Zef)
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
---
|
||||
status: Complete
|
||||
tags: meta
|
||||
---
|
||||
Frontmatter is a common format to attach additional metadata (data about data) to markdown documents.
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
pageDecoration.prefix: "🎄 "
|
||||
---
|
||||
Page decorations allow you to “decorate” pages in various ways.
|
||||
|
||||
For now “various ways” means just one way (adding a visual prefix), but in the future, more such decorations will likely be added.
|
||||
|
||||
There are two ways to decorate a page.
|
||||
|
||||
# Frontmatter
|
||||
The first is demonstrated in the [[Frontmatter]] of this page, by using the special `pageDecoration` attribute.
|
||||
|
||||
# Settings
|
||||
The more useful way is to apply decorations to pages _dynamically_, you can use the `pageDecorations` attribute in [[SETTINGS]].
|
||||
|
||||
Every page decoration has two parts:
|
||||
* `where`: the [[Expression Language]] expression that has to evaluate to `true` for a given page for that decoration to be applied.
|
||||
* A set of decorations to apply, see [[#Supported decorations]]
|
||||
|
||||
For example:
|
||||
```yaml
|
||||
- where: 'tags = "person"'
|
||||
prefix: "🧑 "
|
||||
```
|
||||
|
||||
This will prefix all pages tagged with `#person` with a 🧑 emoji.
|
||||
|
||||
Here on silverbullet.md, we have a decoration like this for pages tagged with #plug: [[Plugs/Emoji]] and [[Plugs/Git]] for instance.
|
||||
|
||||
# Supported decorations
|
||||
For now there’s just one:
|
||||
|
||||
* `prefix`: A (visual) string prefix (often an emoji) to add to all page names. This prefix will appear in the top bar as well as in (live preview) links to this page. For example, the name of this page is actually “Page Decorations”, but when you link to it, you’ll see it’s prefixed with a 🎄: [[Page Decorations]]
|
||||
|
||||
Again — later, more such decorations may be added.
|
||||
@@ -48,6 +48,11 @@ shortcuts:
|
||||
- command: "{[Upload: File]}"
|
||||
priority: 1 # Make sure this appears at the top of the list in the command palette
|
||||
|
||||
# Page decorations
|
||||
pageDecorations:
|
||||
- where: 'tags = "plug"'
|
||||
prefix: "🔌 "
|
||||
|
||||
# Defines files to ignore in a format compatible with .gitignore
|
||||
spaceIgnore: |
|
||||
dist
|
||||
|
||||
Reference in New Issue
Block a user