1
0
This commit is contained in:
Zef Hemel 2023-12-19 17:20:47 +01:00
parent e8acd1dbfb
commit a5c7405a35
6 changed files with 34 additions and 11 deletions

View File

@ -16,7 +16,7 @@ export type MarkdownRenderOptions = {
attachmentUrlPrefix?: string; attachmentUrlPrefix?: string;
preserveAttributes?: true; preserveAttributes?: true;
// When defined, use to inline images as data: urls // When defined, use to inline images as data: urls
translateUrls?: (url: string) => string; translateUrls?: (url: string, type: "link" | "image") => string;
}; };
function cleanTags(values: (Tag | null)[], cleanWhitespace = false): Tag[] { function cleanTags(values: (Tag | null)[], cleanWhitespace = false): Tag[] {
@ -466,11 +466,11 @@ export function renderMarkdownToHtml(
return; return;
} }
if (t.name === "img") { if (t.name === "img") {
t.attrs!.src = options.translateUrls!(t.attrs!.src!); t.attrs!.src = options.translateUrls!(t.attrs!.src!, "image");
} }
if (t.name === "a" && t.attrs!.href) { if (t.name === "a" && t.attrs!.href) {
t.attrs!.href = options.translateUrls!(t.attrs!.href); t.attrs!.href = options.translateUrls!(t.attrs!.href, "link");
} }
}); });
} }

View File

@ -2,6 +2,7 @@ import { asset, clientStore, editor, markdown, system } from "$sb/syscalls.ts";
import { renderMarkdownToHtml } from "./markdown_render.ts"; import { renderMarkdownToHtml } from "./markdown_render.ts";
import { resolvePath } from "$sb/lib/resolve.ts"; import { resolvePath } from "$sb/lib/resolve.ts";
import { expandCodeWidgets } from "./api.ts"; import { expandCodeWidgets } from "./api.ts";
import { folderName, resolve } from "../../common/path.ts";
export async function updateMarkdownPreview() { export async function updateMarkdownPreview() {
if (!(await clientStore.get("enableMarkdownPreview"))) { if (!(await clientStore.get("enableMarkdownPreview"))) {
@ -18,9 +19,14 @@ export async function updateMarkdownPreview() {
const html = renderMarkdownToHtml(mdTree, { const html = renderMarkdownToHtml(mdTree, {
smartHardBreak: true, smartHardBreak: true,
annotationPositions: true, annotationPositions: true,
translateUrls: (url) => { translateUrls: (url, type) => {
if (!url.includes("://")) { if (!url.includes("://")) {
url = resolvePath(currentPage, decodeURI(url), true); if (type === "image" && !url.startsWith("/")) {
// Make relative to current folder
url = resolve(folderName(currentPage), decodeURI(url));
} else if (type === "link") { // link
url = resolvePath(currentPage, decodeURI(url), true);
}
} }
return url; return url;
}, },

View File

@ -9,6 +9,7 @@ import { decoratorStateField } from "./util.ts";
import type { Client } from "../client.ts"; import type { Client } from "../client.ts";
import { resolvePath } from "$sb/lib/resolve.ts"; import { resolvePath } from "$sb/lib/resolve.ts";
import { folderName, resolve } from "../../common/path.ts";
class InlineImageWidget extends WidgetType { class InlineImageWidget extends WidgetType {
constructor( constructor(
@ -34,7 +35,6 @@ class InlineImageWidget extends WidgetType {
const img = document.createElement("img"); const img = document.createElement("img");
let url = this.url; let url = this.url;
url = resolvePath(this.client.currentPage!, url, true); url = resolvePath(this.client.currentPage!, url, true);
console.log("Resolving image to", url);
// console.log("Creating DOM", this.url); // console.log("Creating DOM", this.url);
const cachedImageHeight = this.client.space.getCachedImageHeight(url); const cachedImageHeight = this.client.space.getCachedImageHeight(url);
img.onload = () => { img.onload = () => {
@ -76,8 +76,12 @@ export function inlineImagesPlugin(client: Client) {
let url = imageRexexResult.groups.url; let url = imageRexexResult.groups.url;
const title = imageRexexResult.groups.title; const title = imageRexexResult.groups.title;
if (url.indexOf("://") === -1) {
url = decodeURI(url); const currentPage = client.currentPage!;
const currentFolder = folderName(currentPage);
if (url.indexOf("://") === -1 && !url.startsWith("/")) {
url = resolve(currentFolder, decodeURI(url));
} }
widgets.push( widgets.push(
Decoration.widget({ Decoration.widget({

View File

@ -10,6 +10,7 @@ import { ParseTree } from "$sb/lib/tree.ts";
import { lezerToParseTree } from "../../common/markdown_parser/parse_tree.ts"; import { lezerToParseTree } from "../../common/markdown_parser/parse_tree.ts";
import type { Client } from "../client.ts"; import type { Client } from "../client.ts";
import { resolvePath } from "$sb/lib/resolve.ts"; import { resolvePath } from "$sb/lib/resolve.ts";
import { folderName, resolve } from "../../common/path.ts";
class TableViewWidget extends WidgetType { class TableViewWidget extends WidgetType {
constructor( constructor(
@ -38,10 +39,16 @@ class TableViewWidget extends WidgetType {
// Annotate every element with its position so we can use it to put // Annotate every element with its position so we can use it to put
// the cursor there when the user clicks on the table. // the cursor there when the user clicks on the table.
annotationPositions: true, annotationPositions: true,
translateUrls: (url) => { translateUrls: (url, type) => {
if (!url.includes("://")) { if (!url.includes("://")) {
url = resolvePath(this.editor.currentPage!, decodeURI(url), true); if (type === "image" && !url.startsWith("/")) {
// Make relative to current folder
url = resolve(folderName(this.editor.currentPage!), decodeURI(url));
} else if (type === "link") { // link
url = resolvePath(this.editor.currentPage!, decodeURI(url), true);
}
} }
return url; return url;
}, },
preserveAttributes: true, preserveAttributes: true,

View File

@ -11,6 +11,12 @@ export default function reducer(
...state, ...state,
isLoading: true, isLoading: true,
currentPage: action.name, currentPage: action.name,
panels: {
...state.panels,
// Hide these by default to avoid flickering
top: {},
bottom: {},
},
}; };
case "page-loaded": case "page-loaded":
return { return {

View File

@ -1,4 +1,4 @@
Tags in SilverBullet are used to encode types of [[Objects]]. Tags in SilverBullet are used to encode types of [[Objects]].
See [[Objects@tags]] for more information. See [[Objects$tags]] for more information.