Page MenuHomeSealhub

No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
diff --git a/src/index.ts b/src/index.ts
index 0db40db..ed569d2 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,19 +1,20 @@
export * from "./component.js";
export * from "./component-arguments/component-argument.js";
export * as ComponentArguments from "./component-arguments/component-arguments.js";
export * from "./component-arguments/component-arguments.js"; // exporting this also as root elements to make it easier to auto-import those
export * from "./jdd-context.js";
export * from "./registry.js";
export { insert_nbsp } from "./utils/insert_nbsp.js";
+export { hyphenate } from "./utils/hyphenate.js";
export * from "./document.js";
export * from "./jdd.js";
import type * as Sealious from "sealious";
declare module "koa" {
interface BaseContext {
$context: Sealious.Context;
// omitting $app, as it needs to be assigned in the final repo, with TheApp
$body: Record<string, unknown>;
}
}
diff --git a/src/jdd-context.ts b/src/jdd-context.ts
index cb950f8..48e39fa 100644
--- a/src/jdd-context.ts
+++ b/src/jdd-context.ts
@@ -1,111 +1,112 @@
import {
FileManager,
FilePointer,
PathFilePointer,
} from "@sealcode/file-manager";
import type { KoaResponsiveImageRouter } from "koa-responsive-image-router";
import { Renderer, marked } from "marked";
import { FlatTemplatable } from "tempstream";
import { insert_nbsp } from "./utils/insert_nbsp.js";
-import { get_hyphenator } from "./hyphenation.js";
import { Token } from "marked";
import slug from "slug";
import { decode } from "html-entities";
import type * as Koa from "koa";
import { Registry } from "./registry.js";
+import { hyphenate } from "./utils/hyphenate.js";
export type JDDFreshValueMode = "empty" | "example";
export interface JDDContext {
render_image: (
file_id: FilePointer | string | null,
args: Parameters<KoaResponsiveImageRouter["image"]>[1]
) => FlatTemplatable;
render_markdown: (
language: string,
markdown: string
) => string | Promise<string>;
hyphenate: (language: string, text: string) => Promise<string>;
encode_file: (photo: FilePointer, persistent: boolean) => Promise<string>;
decode_file: (token: string) => Promise<PathFilePointer | null>;
file_manager: FileManager;
language: string;
ctx: Koa.Context;
registry: Registry;
fresh_value_mode: JDDFreshValueMode;
}
class RendererWithHeadings extends Renderer {
heading(text: string, depth: number): string {
const id = slug(decode(text));
return /* HTML */ ` <h${depth} id="${id}">
<a class="anchor" href="#${id}">
<span class="markdown-header-link"></span>
</a>
${text}
</h${depth}>`;
}
}
export const makeSimpleJDDContext: (
file_manager: FileManager,
registry: Registry,
fresh_value_mode: JDDFreshValueMode
) => Omit<JDDContext, "language"> = (
file_manager,
registry,
fresh_value_mode = "empty"
) => ({
registry,
fresh_value_mode,
render_image: async (file) => {
if (typeof file == "string") {
file = await file_manager.fromToken(file);
}
const path = (await file?.getPath()) || "/some-path";
return file ? /* HTML */ `<img src="file://${path}}" />` : "";
},
hyphenate: async (language: string, text: string) => {
- const hyphenator = await get_hyphenator(language);
- return hyphenator(text);
+ return hyphenate(language, text);
},
render_markdown: async (language, string) => {
- const hyphenator = await get_hyphenator(language);
return string
? marked.parse(string.toString(), {
walkTokens: (token: Token) => {
if (!("text" in token) || token.type === "code") return;
token.text = insert_nbsp(token.text as string);
if (token.type == "text") {
- token.text = hyphenator(token.text as string);
+ token.text = hyphenate(
+ language,
+ token.text as string
+ );
}
},
renderer: new RendererWithHeadings(),
})
: "";
},
encode_file: (photo: FilePointer, persistent: boolean) => {
return photo.save(persistent);
},
decode_file: (token: string) => {
return file_manager.fromToken(token);
},
file_manager,
ctx: {} as unknown as Koa.Context,
});
export const makeSimpleEnglishJDDContext: (args?: {
file_manager?: FileManager;
registry?: Registry;
}) => JDDContext = (
{
file_manager = new FileManager("/tmp", "/uploaded_files"),
registry = new Registry(),
} = {
file_manager: new FileManager("/tmp", "/uploaded_files"),
registry: new Registry(),
}
) => ({
...makeSimpleJDDContext(file_manager, registry, "empty"),
language: "en-us",
});

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 28, 02:13 (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1230009
Default Alt Text
(4 KB)

Event Timeline