1
0
This commit is contained in:
Zef Hemel 2024-01-25 10:17:42 +01:00
parent 2893fe11a3
commit 232a0d8df8
7 changed files with 145 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import {
} from "$sb/lib/tree.ts";
import { resolveAttachmentPath, resolvePath } from "$sb/lib/resolve.ts";
import { parsePageRef } from "$sb/lib/page.ts";
import { tagPrefix } from "../index/constants.ts";
async function actionClickOrActionEnter(
mdTree: ParseTree | null,
@ -27,6 +28,7 @@ async function actionClickOrActionEnter(
"Link",
"CommandLink",
"PageRef",
"Hashtag",
]
.includes(
t.type!,
@ -96,6 +98,16 @@ async function actionClickOrActionEnter(
}
break;
}
case "Hashtag": {
console.log("Got myself a hash tag", mdTree);
const hashtag = mdTree.children![0].text!.slice(1);
await editor.navigate(
{ page: `${tagPrefix}${hashtag}`, pos: 0 },
false,
inNewWindow,
);
break;
}
}
}

1
plugs/index/constants.ts Normal file
View File

@ -0,0 +1 @@
export const tagPrefix = "📌 ";

View File

@ -167,3 +167,22 @@ functions:
path: lint.ts:lintYAML
events:
- editor:lint
# Tag file system
readFileTag:
path: tag_page.ts:readFileTag
pageNamespace:
pattern: "📌 .+"
operation: readFile
writePageTag:
path: tag_page.ts:writeFileTag
pageNamespace:
pattern: "📌 .+"
operation: readFile
getPageMetaTag:
path: tag_page.ts:getFileMetaTag
pageNamespace:
pattern: "📌 .+"
operation: getFileMeta

92
plugs/index/tag_page.ts Normal file
View File

@ -0,0 +1,92 @@
import { FileMeta } from "$sb/types.ts";
import { markdown, system } from "$sb/syscalls.ts";
import { renderToText } from "$sb/lib/tree.ts";
import { tagPrefix } from "./constants.ts";
export async function readFileTag(
name: string,
): Promise<{ data: Uint8Array; meta: FileMeta }> {
const tagName = name.substring(
tagPrefix.length,
name.length - ".md".length,
);
const text = `These are all objects in your space tagged with #${tagName}.
\`\`\`template
template: |
{{#if .}}
# Pages
{{#each .}}
* [[{{name}}]]
{{/each}}
{{/if}}
query: |
page where tags = "${tagName}"
\`\`\`
\`\`\`template
template: |
{{#if .}}
# Tasks
{{#each .}}
* [{{state}}] [[{{ref}}]] {{name}}
{{/each}}
{{/if}}
query: |
task where tags = "${tagName}"
\`\`\`
\`\`\`template
template: |
{{#if .}}
# Items
{{#each .}}
* [[{{ref}}]] {{name}}
{{/each}}
{{/if}}
query: |
item where tags = "${tagName}"
\`\`\`
\`\`\`template
template: |
{{#if .}}
# Paragraphs
{{#each .}}
* [[{{ref}}]] {{text}}
{{/each}}
{{/if}}
query: |
paragraph where tags = "${tagName}"
\`\`\`
`;
let tree = await markdown.parseMarkdown(text);
tree = await system.invokeFunction("markdown.expandCodeWidgets", tree, name);
return {
data: new TextEncoder().encode(renderToText(tree)),
meta: {
name,
contentType: "text/markdown",
size: text.length,
created: 0,
lastModified: 0,
perm: "ro",
},
};
}
export function writeFileTag(
name: string,
): FileMeta {
// Never actually writing this
return getFileMetaTag(name);
}
export function getFileMetaTag(name: string): FileMeta {
return {
name,
contentType: "text/markdown",
size: -1,
created: 0,
lastModified: 0,
perm: "ro",
};
}

View File

@ -6,6 +6,7 @@ import { resolveAttachmentPath } from "$sb/lib/resolve.ts";
import { parse } from "../../common/markdown_parser/parse_tree.ts";
import { parsePageRef } from "$sb/lib/page.ts";
import { extendedMarkdownLanguage } from "../../common/markdown_parser/parser.ts";
import { tagPrefix } from "../../plugs/index/constants.ts";
const activeWidgets = new Set<MarkdownWidget>();
@ -157,6 +158,24 @@ export class MarkdownWidget extends WidgetType {
});
});
// Attach click handlers to hash tags
div.querySelectorAll("span.hashtag").forEach((el_) => {
const el = el_ as HTMLElement;
// Override default click behavior with a local navigate (faster)
console.log("Found hashtag", el.innerText);
el.addEventListener("click", (e) => {
console.log("Hashtag clicked", el.innerText);
if (e.ctrlKey || e.metaKey) {
// Don't do anything special for ctrl/meta clicks
return;
}
this.client.navigate({
page: `${tagPrefix}${el.innerText.slice(1)}`,
pos: 0,
});
});
});
div.querySelectorAll("button[data-onclick]").forEach((el_) => {
const el = el_ as HTMLElement;
const onclick = el.dataset.onclick!;

View File

@ -177,6 +177,7 @@
color: var(--editor-hashtag-color);
background-color: var(--editor-hashtag-background-color);
border: 1px solid var(--editor-hashtag-border-color);
cursor: pointer;
}
.sb-line-hr {

View File

@ -6,6 +6,7 @@ release.
## Edge
_The changes below are not yet released “properly”. To them out early, check out [the docs on edge](https://community.silverbullet.md/t/living-on-the-edge-builds/27)._
* Tag pages: when you click on a #tag you will now be directed to a page that shows all pages, tasks, items and paragraphs tagged with that tag.
* Bug fixes:
* Improved Ctrl/Cmd-click (to open links in a new window) behavior: now actually follow `@pos` and `$anchor` links.
* Right-clicking links now opens browser native context menu again