Replaced #inst with #template and #include, made query parsing more robust

pull/50/head
Zef Hemel 2022-07-25 16:51:46 +02:00
parent f75915535f
commit f9875e7e57
10 changed files with 168 additions and 70 deletions

View File

@ -184,6 +184,22 @@ functions:
<!-- #query |^| -->
<!-- /query -->
insertInclude:
path: "./template.ts:insertTemplateText"
slashCommand:
name: include
value: |
<!-- #include "|^|" -->
<!-- /include -->
insertInlineTemplate:
path: "./template.ts:insertTemplateText"
slashCommand:
name: inline-template
value: |
<!-- #template "|^|" {} -->
<!-- /template -->
quickNoteCommand:
path: ./template.ts:quickNoteCommand
command:

View File

@ -30,11 +30,22 @@ export async function queryComplete() {
};
}
prefix = await matchBefore('#inst "[^"]*');
prefix = await matchBefore('#template "[^"]*');
if (prefix) {
let allPages = await listPages();
return {
from: prefix.from + '#inst "'.length,
from: prefix.from + '#template "'.length,
options: allPages.map((pageMeta) => ({
label: pageMeta.name,
})),
};
}
prefix = await matchBefore('#include "[^"]*');
if (prefix) {
let allPages = await listPages();
return {
from: prefix.from + '#include "'.length,
options: allPages.map((pageMeta) => ({
label: pageMeta.name,
})),

View File

@ -16,6 +16,8 @@ import { replaceTemplateVars } from "../core/template";
import { jsonToMDTable, queryRegex } from "./util";
import { dispatch } from "@plugos/plugos-syscall/event";
import { replaceAsync } from "../lib/util";
import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown";
import { nodeAtPos } from "@silverbulletmd/common/tree";
export async function updateMaterializedQueriesCommand() {
const currentPage = await getCurrentPage();
@ -32,7 +34,7 @@ export async function updateMaterializedQueriesCommand() {
}
export const templateInstRegex =
/(<!--\s*#inst\s+"([^"]+)"(.+?)-->)(.+?)(<!--\s*\/inst\s*-->)/gs;
/(<!--\s*#(template|include)\s+"([^"]+)"(.+?)-->)(.+?)(<!--\s*\/\2\s*-->)/gs;
async function updateTemplateInstantiations(
text: string,
@ -41,7 +43,7 @@ async function updateTemplateInstantiations(
return replaceAsync(
text,
templateInstRegex,
async (fullMatch, startInst, template, args, body, endInst) => {
async (fullMatch, startInst, type, template, args, body, endInst) => {
args = args.trim();
let parsedArgs = {};
if (args) {
@ -52,12 +54,26 @@ async function updateTemplateInstantiations(
return fullMatch;
}
}
let { text: templateText } = await readPage(template);
let templateFn = Handlebars.compile(
replaceTemplateVars(templateText, pageName),
{ noEscape: true }
);
let newBody = templateFn(parsedArgs);
let templateText = "";
if (template.startsWith("http://") || template.startsWith("https://")) {
try {
let req = await fetch(template);
templateText = await req.text();
} catch (e: any) {
templateText = `ERROR: ${e.message}`;
}
} else {
templateText = (await readPage(template)).text;
}
let newBody = templateText;
// if it's a template (note a literal "include")
if (type === "template") {
let templateFn = Handlebars.compile(
replaceTemplateVars(templateText, pageName),
{ noEscape: true }
);
newBody = templateFn(parsedArgs);
}
return `${startInst}\n${newBody.trim()}\n${endInst}`;
}
);
@ -79,10 +95,19 @@ export async function updateMaterializedQueriesOnPage(
return false;
}
let newText = await updateTemplateInstantiations(text, pageName);
let tree = await parseMarkdown(newText);
newText = await replaceAsync(
newText,
queryRegex,
async (fullMatch, startQuery, query, body, endQuery) => {
async (fullMatch, startQuery, query, body, endQuery, index) => {
let currentNode = nodeAtPos(tree, index + 1);
if (currentNode?.type !== "CommentBlock") {
// If not a comment block, it's likely a code block, ignore
return fullMatch;
}
// console.log("Text slice", newText.substring(index, index + 100));
let parsedQuery = parseQuery(replaceTemplateVars(query, pageName));
console.log("Parsed query", parsedQuery);

View File

@ -5,6 +5,24 @@ repo: https://github.com/Willyfrog/silverbullet-backlinks
author: Guillermo Vayá
```
<!-- #include "https://raw.githubusercontent.com/Willyfrog/silverbullet-backlinks/main/README.md" -->
# SilverBullet plug for Backlinks
Provides access to pages that link to the one currently being edited.
Use the `Show Backlinks for current page` command to toggle.
## Wait, SilverBullet?
If you don't know what it is, check its [webpage](https://silverbullet.md), but if
you want me to spoil the fun: it is an extensible note taking app with markdown and plain files at its core
(well... there is a bit of magic in there too, but what good it would be without a little magic?)
## Installation
Open (`cmd+k`) your `PLUGS` note in SilverBullet and add this plug to the list:
```yaml
- https://github.com/Willyfrog/silverbullet-backlinks/releases/download/v0.2/backlinks.plug.json
```
Then run the `Plugs: Update` command and off you go!
<!-- /include -->

View File

@ -4,10 +4,10 @@ uri: github:silverbulletmd/silverbullet-ghost/ghost.plug.json
repo: https://github.com/silverbulletmd/silverbullet-ghost
author: Zef Hemel
```
<!-- #include "https://raw.githubusercontent.com/silverbulletmd/silverbullet-ghost/main/README.md" -->
# Ghost plug for Silver Bullet
Very basic plug to publish pages and posts onto the [Ghost](https://ghost.org) platform
## Configuration
Note: Still very basic, to use:
In your `SETTINGS` specify the following settings:
@ -25,4 +25,14 @@ And in your `SECRETS` file:
This will assume the naming pattern of `posts/my-post-slug` where the first top-level heading (`# Hello`) will be used as the post title.
Commands to use `Ghost: Publish`
Commands to use `Ghost: Publish`
## Installation
Open your `PLUGS` note in SilverBullet and add this plug to the list:
```
- github:silverbulletmd/silverbullet-ghost/ghost.plug.json
```
Then run the `Plugs: Update` command and off you go!
<!-- /include -->

View File

@ -4,15 +4,27 @@ uri: github:silverbulletmd/silverbullet-github/github.plug.json
repo: https://github.com/silverbulletmd/silverbullet-github
author: Zef Hemel
```
<!-- #include "https://raw.githubusercontent.com/silverbulletmd/silverbullet-git/main/README.md" -->
# SilverBullet plug for Git
Very basic in functionality, it assumes you have git configured for push and pull in your space. What it does, roughly speaking:
The git plug provides very basic “git sync” functionality, it assumes you have git configured for push and pull in your space. It offers two commands:
`Git : Sync`:
* Adds all *.md files in your folder to git
* It commits them with a "Snapshot" commit message
* It `git pull`s changes from the remote server
* It `git push`es changes to the remote server
* `Git : Sync`:
* Adds all *.md files in your folder to git
* It commits them with a "Snapshot" commit message
* It `git pull`s changes from the remote server
* It `git push`es changes to the remote server
`Git: Snapshot`:
* Asks you for a commit message
* Commits
## Installation
Open your `PLUGS` note in SilverBullet and add this plug to the list:
```
- github:silverbulletmd/silverbullet-git/git.plug.json
```
Then run the `Plugs: Update` command and off you go!
<!-- /include -->
* `Git: Snapshot`:
* Asks you for a commit message
* Commits

View File

@ -4,9 +4,30 @@ uri: github:silverbulletmd/silverbullet-github/github.plug.json
repo: https://github.com/silverbulletmd/silverbullet-github
author: Zef Hemel
```
<!-- #include "https://raw.githubusercontent.com/silverbulletmd/silverbullet-github/main/README.md" -->
# SilverBullet plug for Github
Provides Github events, notifications and pull requests as query sources using SB's query mechanism
## Installation
Open your `PLUGS` note in SilverBullet and add this plug to the list:
```
- github:silverbulletmd/silverbullet-github/github.plug.json
```
Then run the `Plugs: Update` command and off you go!
## Configuration
This step is optional for anything but the `gh-notification` source, but without it you may be rate limited by the Github API,
To configure, add a `githubToken` key to your `SECRETS` page, this should be a [personal access token](https://github.com/settings/tokens):
```yaml
githubToken: your-github-token
```
## Query sources
* `gh-event` required filters in the `where` clause:
* `username`: the user whose events to query
* `gh-pull`
@ -29,4 +50,5 @@ Example uses:
Where the `template/gh-pull` looks as follows:
* ({{state}}) [{{title}}]({{html_url}})
* ({{state}}) [{{title}}]({{html_url}})
<!-- /include -->

View File

@ -4,9 +4,19 @@ uri: github:silverbulletmd/silverbullet-mattermost/mattermost.plug.json
repo: https://github.com/silverbulletmd/silverbullet-mattermost
author: Zef Hemel
```
<!-- #include "https://raw.githubusercontent.com/silverbulletmd/silverbullet-mattermost/main/README.md" -->
# Mattermost plug for Silver Bullet
Provides a `mm-saved` query provider (and maybe more in the future). Please follow the installation, configuration sections, and have a look at the example.
## Installation
Open your `PLUGS` note in SilverBullet and add this plug to the list:
```
- github:silverbulletmd/silverbullet-mattermost/mattermost.plug.json
```
Then run the `Plugs: Update` command and off you go!
## Configuration
You need two bits of configuration to make this plug work. In `SETTINGS` provide the `mattermostUrl` and `mattermostDefaultTeam` settings, they default to the following:
@ -40,31 +50,6 @@ Note that the `{[Unsaved]}` "button" when clicked, will unsave the post automati
Example uses (using the `template/mm-saved` template above):
<!-- #query mm-saved order by updatedAt desc limit 5 render "template/mm-saved" -->
[lindy.isherwood](mattermost://community.mattermost.com/private-core/pl/u8ospw14ptg47fystidzudigjw) in **R&D Meeting** at 2022-07-22 {[Unsave]}:
> #### [Meeting Recording](https://mattermost.zoom.us/rec/share/e0CmkZr_1xaW0Zd-7N-saD5fir9pmjJy6-xw4JZ7el7IMIUUUr99FiC2WePmBZDw.HRzSgvBjxhsPGQWo)
>
> Access Passcode: `wq$!BA6N`
---
[harrison](mattermost://community.mattermost.com/core/pl/akuzqwdm4if7fdmajjb94hpm5c) in **** at 2022-07-20 {[Unsave]}:
> Hey Zef. Can we chat about the Affirm issue a bit when I get back? From what I've gathered, Devin's told customer support that the issue in Chromium isn't something we can reasonably fix, and while I don't feel like that's anything I should be worried about since it's out of my area, I keep getting ... (More)
---
[zef.hemel](mattermost://community.mattermost.com/core/pl/k41cfgdbhfg5unm8yx1cjkh8ty) in **** at 2022-07-20 {[Unsave]}:
> I'll have to get back to this tomorrow. Filename says "arm64" though
---
[zef.hemel](mattermost://community.mattermost.com/core/pl/9e1ha9yzzpdm9ffhu4od65yykc) in **** at 2022-07-19 {[Unsave]}:
> Agreed. Thinking what are better indicators than what we already ask. Ill reach out.
---
[zef.hemel](mattermost://community.mattermost.com/private-core/pl/hh79ikgfzb8zmb4ezjuow7a1mw) in **Team: Web Platform** at 2022-07-15 {[Unsave]}:
> @webplatform in yesterdays 1:1 with @harrison we came up with the concept of “theme weeks” to solve our problem of scheduling _important but not urgent_ work. Examples are: looking at performance improvements, cleaning/fixing lingering bugs from our backlog, working on accessibility tickets, perfor ... (More)
---
<!-- /query -->
<!-- /query -->
<!-- /include -->

View File

@ -4,8 +4,18 @@ uri: github:silverbulletmd/silverbullet-mount/mount.plug.json
repo: https://github.com/silverbulletmd/silverbullet-mount
author: Zef Hemel
```
<!-- #include "https://raw.githubusercontent.com/silverbulletmd/silverbullet-mount/main/README.md" -->
# Mounting of external systems into SB
Enables mounting of external folders or SB instances into your space.
Enables mounting of external folders or SB instances into your space mounted under a `🚪` prefix.
## Installation
Open your `PLUGS` note in SilverBullet and add this plug to the list:
```
- github:silverbulletmd/silverbullet-mount/mount.plug.json
```
Then run the `Plugs: Update` command and off you go!
## Configuration
Create a `MOUNTS` page:
@ -19,15 +29,4 @@ Create a `MOUNTS` page:
path: http://some-ip:3000
password: mypassword
```
This will make these available under `🚪 docs/` and `🚪 remote/` respectively.
## Features
* Auto translates internal wiki links (prefixes with prefix) and removes prefix upon save
## To do
* [ ] Handle queries
* `page` and `link` query needs to dynamically add/remove a `and name =~ /^🚪 PREFIX/` clause)
* `task` same but with `page` check
* [x] Add `file:` support
* [x] Add `http:`/`https:` support
<!-- /include -->

View File

@ -4,12 +4,12 @@ Plugs are an extension mechanism (implemented using a library called `plugos` th
## Directory
<!-- #query page where type = “plug” render “template/plug” -->
* [[🔌 Git]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-github))
* [[🔌 Backlinks]] by **Guillermo Vayá** ([repo](https://github.com/Willyfrog/silverbullet-backlinks))
* [[🔌 Ghost]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-ghost))
* [[🔌 Git]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-github))
* [[🔌 Github]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-github))
* [[🔌 Mattermost]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-mattermost))
* [[🔌 Mount]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-mount))
* [[🔌 Backlinks]] by **Guillermo Vayá** ([repo](https://github.com/Willyfrog/silverbullet-backlinks))
<!-- /query -->
## How to develop your own plug