From 69ec24ee2d7916de1685932ca3ca6077c717095e Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 2 Sep 2022 15:41:40 +0200 Subject: [PATCH] Change search prefix and add cloud page references --- packages/plugs/core/cloud.ts | 69 ++++++++++++++++++++++++++++++ packages/plugs/core/core.plug.yaml | 20 +++++++-- packages/plugs/core/search.ts | 6 ++- packages/web/styles/theme.scss | 9 +--- website/CHANGELOG.md | 3 ++ 5 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 packages/plugs/core/cloud.ts diff --git a/packages/plugs/core/cloud.ts b/packages/plugs/core/cloud.ts new file mode 100644 index 00000000..37358716 --- /dev/null +++ b/packages/plugs/core/cloud.ts @@ -0,0 +1,69 @@ +import { + renderToText, + replaceNodesMatching, +} from "@silverbulletmd/common/tree"; +import { PageMeta } from "@silverbulletmd/common/types"; +import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown"; + +const pagePrefix = "💭 "; + +export async function readPageCloud( + name: string +): Promise<{ text: string; meta: PageMeta } | undefined> { + let originalUrl = name.substring(pagePrefix.length); + let url = originalUrl; + if (!url.includes("/")) { + url += "/index"; + } + if (!url.startsWith("127.0.0.1")) { + url = `https://${url}`; + } else { + url = `http://${url}`; + } + let text = ""; + try { + let r = await fetch(`${url}.md`); + text = await r.text(); + if (r.status !== 200) { + text = `ERROR: ${text}`; + } + } catch (e: any) { + console.error("ERROR", e.message); + text = e.message; + } + return { + text: await translateLinksWithPrefix( + text, + `${pagePrefix}${originalUrl.split("/")[0]}/` + ), + meta: { + name, + lastModified: 0, + perm: "ro", + }, + }; +} + +async function translateLinksWithPrefix( + text: string, + prefix: string +): Promise { + let tree = await parseMarkdown(text); + replaceNodesMatching(tree, (tree) => { + if (tree.type === "WikiLinkPage") { + // Add the prefix in the link text + tree.children![0].text = prefix + tree.children![0].text; + } + return undefined; + }); + text = renderToText(tree); + return text; +} + +export async function getPageMetaCloud(name: string): Promise { + return { + name, + lastModified: 0, + perm: "ro", + }; +} diff --git a/packages/plugs/core/core.plug.yaml b/packages/plugs/core/core.plug.yaml index f3b6d1ea..18c46d27 100644 --- a/packages/plugs/core/core.plug.yaml +++ b/packages/plugs/core/core.plug.yaml @@ -149,12 +149,12 @@ functions: readPageSearch: path: ./search.ts:readPageSearch pageNamespace: - pattern: "@search/.+" + pattern: "🔍 .+" operation: readPage getPageMetaSearch: path: ./search.ts:getPageMetaSearch pageNamespace: - pattern: "@search/.+" + pattern: "🔍 .+" operation: getPageMeta # Template commands @@ -367,5 +367,17 @@ functions: path: ./stats.ts:statsCommand command: name: "Stats: Show" - key: "Ctrl-Shift-s" - mac: "Cmd-Shift-s" + key: "Ctrl-s" + mac: "Cmd-s" + + # Cloud pages + readPageCloud: + path: ./cloud.ts:readPageCloud + pageNamespace: + pattern: "💭 .+" + operation: readPage + getPageMetaCloud: + path: ./cloud.ts:getPageMetaCloud + pageNamespace: + pattern: "💭 .+" + operation: getPageMeta diff --git a/packages/plugs/core/search.ts b/packages/plugs/core/search.ts index ca1c07ea..d71ba6cc 100644 --- a/packages/plugs/core/search.ts +++ b/packages/plugs/core/search.ts @@ -14,6 +14,8 @@ import { IndexTreeEvent } from "@silverbulletmd/web/app_event"; import { applyQuery, QueryProviderEvent } from "../query/engine"; import { removeQueries } from "../query/util"; +const searchPrefix = "🔍 "; + export async function index(data: IndexTreeEvent) { removeQueries(data.tree); let cleanText = renderToText(data.tree); @@ -55,14 +57,14 @@ export async function queryProvider({ export async function searchCommand() { let phrase = await prompt("Search for: "); if (phrase) { - await navigate(`@search/${phrase}`); + await navigate(`${searchPrefix}${phrase}`); } } export async function readPageSearch( name: string ): Promise<{ text: string; meta: PageMeta }> { - let phrase = name.substring("@search/".length); + let phrase = name.substring(searchPrefix.length); let results = await fullTextSearch(phrase, 100); const text = `# Search results for "${phrase}"\n${results .map((r: any) => `* [[${r.name}]] (score: ${r.rank})`) diff --git a/packages/web/styles/theme.scss b/packages/web/styles/theme.scss index f2cf8efa..bbe50189 100644 --- a/packages/web/styles/theme.scss +++ b/packages/web/styles/theme.scss @@ -105,13 +105,8 @@ padding: 2px 2px !important; } -.sb-line-h1 .sb-meta { - color: orange; -} -.sb-line-h2 .sb-meta { - color: orange; -} - +.sb-line-h1 .sb-meta, +.sb-line-h2 .sb-meta, .sb-line-h3 .sb-meta { color: orange; } diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index f9de47e6..118558e8 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -1,5 +1,8 @@ An attempt at documenting of the changes/new features introduced in each (pre) release. +## 0.0.33 +* Changed full-text search page prefix from `@search/` to `🔍` for the {[Search Space]} command. + ## 0.0.32 * **Inline image previews**: use the standard `![alt text](https://url.com/image.jpg)` notation and a preview of the image will appear automatically. Example: ![Inline image preview](https://user-images.githubusercontent.com/812886/186218876-6d8a4a71-af8b-4e9e-83eb-4ac89607a6b4.png)