-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial attempt to fix #560 #905
base: main
Are you sure you want to change the base?
Changes from all commits
6630f0e
9e9b071
2ec3508
7ff12ca
cd31952
1a8cfd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,15 @@ import { | |
import { cacheFileListing } 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 type { Decoration } from "$lib/web.ts"; | ||
|
||
let decorations: Decoration[] = []; | ||
|
||
// Completion | ||
export async function pageComplete(completeEvent: CompleteEvent) { | ||
updateDecoratorConfig(); | ||
|
||
// Try to match [[wikilink]] | ||
let isWikilink = true; | ||
let match = /\[\[([^\]@$#:\{}]*)$/.exec(completeEvent.linePrefix); | ||
|
@@ -79,10 +85,19 @@ export async function pageComplete(completeEvent: CompleteEvent) { | |
from: completeEvent.pos - match[1].length, | ||
options: allPages.map((pageMeta) => { | ||
const completions: any[] = []; | ||
let namePrefix = ""; | ||
const decor = decorations?.filter(d => pageMeta.tags?.some((t: any) => d.tag === t)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And maybe use |
||
if (decor === undefined || decor.length == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And without the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And when switching to |
||
namePrefix = ""; | ||
} else { | ||
namePrefix = decor[0].prefix; | ||
} | ||
if (isWikilink) { | ||
if (pageMeta.displayName) { | ||
const decoratedName = namePrefix + pageMeta.displayName; | ||
completions.push({ | ||
label: `${pageMeta.displayName}`, | ||
displayLabel: `${decoratedName}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about just |
||
boost: new Date(pageMeta.lastModified).getTime(), | ||
apply: pageMeta.tag === "template" | ||
? pageMeta.name | ||
|
@@ -93,8 +108,10 @@ export async function pageComplete(completeEvent: CompleteEvent) { | |
} | ||
if (Array.isArray(pageMeta.aliases)) { | ||
for (const alias of pageMeta.aliases) { | ||
const decoratedName = namePrefix + alias; | ||
completions.push({ | ||
label: `${alias}`, | ||
displayLabel: `${decoratedName}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
boost: new Date(pageMeta.lastModified).getTime(), | ||
apply: pageMeta.tag === "template" | ||
? pageMeta.name | ||
|
@@ -104,8 +121,10 @@ export async function pageComplete(completeEvent: CompleteEvent) { | |
}); | ||
} | ||
} | ||
const decoratedName = namePrefix + pageMeta.name; | ||
completions.push({ | ||
label: `${pageMeta.name}`, | ||
displayLabel: `${decoratedName}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same again |
||
boost: new Date(pageMeta.lastModified).getTime(), | ||
type: "page", | ||
}); | ||
|
@@ -132,6 +151,7 @@ export async function pageComplete(completeEvent: CompleteEvent) { | |
}; | ||
} | ||
|
||
|
||
function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta { | ||
const name = fileMeta.name.substring(0, fileMeta.name.length - 3); | ||
return { | ||
|
@@ -143,3 +163,17 @@ 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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import { | |
import { encodePageRef, parsePageRef } from "$sb/lib/page_ref.ts"; | ||
import { Fragment, renderHtml, Tag } from "./html_render.ts"; | ||
import { isLocalPath } from "$sb/lib/resolve.ts"; | ||
import { PageMeta } from "$sb/types.ts"; | ||
|
||
export type MarkdownRenderOptions = { | ||
failOnUnknown?: true; | ||
|
@@ -554,25 +555,34 @@ function traverseTag( | |
export function renderMarkdownToHtml( | ||
t: ParseTree, | ||
options: MarkdownRenderOptions = {}, | ||
allPages: PageMeta[] = [], | ||
) { | ||
preprocess(t); | ||
const htmlTree = posPreservingRender(t, options); | ||
if (htmlTree && options.translateUrls) { | ||
if (htmlTree) { | ||
traverseTag(htmlTree, (t) => { | ||
if (typeof t === "string") { | ||
return; | ||
} | ||
if (t.name === "img") { | ||
if (t.name === "img" && options.translateUrls) { | ||
t.attrs!.src = options.translateUrls!(t.attrs!.src!, "image"); | ||
} | ||
|
||
if (t.name === "a" && t.attrs!.href) { | ||
t.attrs!.href = options.translateUrls!(t.attrs!.href, "link"); | ||
if (options.translateUrls) { | ||
t.attrs!.href = options.translateUrls!(t.attrs!.href, "link"); | ||
} | ||
if (t.attrs!["data-ref"]?.length) { | ||
const pageMeta = allPages.filter(p => t.attrs!["data-ref"]!.startsWith(p.name)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
if (pageMeta) { | ||
t.body = [(pageMeta[0]?.namePrefix ?? "") + t.body] | ||
} | ||
} | ||
if (t.body.length === 0) { | ||
t.body = [t.attrs!.href]; | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
return renderHtml(htmlTree); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,9 +67,9 @@ export function cleanWikiLinkPlugin(client: Client) { | |
} | ||
return; | ||
} | ||
|
||
const pageMeta = client.ui.viewState.allPages.filter(p => p.name == url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -> |
||
const linkText = alias || | ||
(url.includes("/") ? url.split("/").pop()! : url); | ||
(pageMeta[0]?.namePrefix ?? "") + (url.includes("/") ? url.split("/").pop()! : url); | ||
|
||
// And replace it with a widget | ||
widgets.push( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { PageMeta } from "../plug-api/types.ts"; | ||
import { Action, AppViewState } from "../type/web.ts"; | ||
import { PageState } from "./navigator.ts"; | ||
|
||
export default function reducer( | ||
state: AppViewState, | ||
|
@@ -20,6 +22,13 @@ export default function reducer( | |
}; | ||
case "page-loaded": { | ||
const mouseDetected = window.matchMedia("(any-pointer:fine)").matches; | ||
const pageMeta = state.allPages.filter(p => p.name == action.meta.name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const decor = state.settings.decorations?.filter(d => pageMeta[0]?.tags?.some(t => d.tag === t)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (decor === undefined || decor.length == 0) { | ||
// Do nothing | ||
} else { | ||
action.meta.namePrefix = decor[0].prefix; | ||
} | ||
return { | ||
...state, | ||
isLoading: false, | ||
|
@@ -58,16 +67,31 @@ 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; | ||
} | ||
for (const pageMeta of action.allPages) { | ||
const oldPageMetaItem = oldPageMeta.get(pageMeta.name); | ||
if (oldPageMetaItem && oldPageMetaItem.lastOpened) { | ||
pageMeta.lastOpened = oldPageMetaItem.lastOpened; | ||
} | ||
let namePrefix = ""; | ||
const decor = state.settings.decorations?.filter(d => pageMeta.tags?.some((t: any) => d.tag === t)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (decor === undefined || decor.length == 0) { | ||
namePrefix = ""; | ||
} else { | ||
namePrefix = decor[0].prefix; | ||
} | ||
pageMeta.namePrefix = namePrefix; | ||
if (pageMeta.name === state.currentPage) { | ||
currPageMeta!.namePrefix = namePrefix; | ||
} | ||
} | ||
|
||
return { | ||
...state, | ||
allPages: action.allPages, | ||
currentPageMeta: currPageMeta, | ||
}; | ||
} | ||
case "start-navigate": { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
await
this so that you can be suredecorations
will have a value from the first invocation (and you catch errors, which otherwise would go into the void).