Page MenuHomeSealhub

No OneTemporary

diff --git a/package.json b/package.json
index b4cbc5e..8467106 100644
--- a/package.json
+++ b/package.json
@@ -1,61 +1,61 @@
{
"name": "@sealcode/jdd",
- "version": "0.6.4-beta2",
+ "version": "0.6.4-beta3",
"description": "JSON-Driven Documents",
"main": "lib/index.js",
"type": "module",
"scripts": {
"pretest": "npm run build",
"test": "node test.cjs",
"prebuild": "npm run lint",
"build": "tsc",
"typecheck": "tsc --noemit",
"prepare": "npm run build",
"lint": "eslint src",
"preinstrument": "npm run build && rm -fr .xunit coverage lib-instrumented",
"instrument": "npx nyc instrument --exclude \"\" lib lib-instrumented",
"pretest-reports": "npm run instrument",
"test-reports": "npx nyc --exclude \"\" ./node_modules/.bin/mocha --recursive --timeout=10000 --require source-map-support/register --reporter xunit --reporter-option output=.xunit 'lib-instrumented/**/*.test.js' && nyc report --reporter clover --exclude \"\"",
"precover-html": "rm -rf coverate/lcov-report",
"cover-html": "npm run test-reports && nyc report --reporter lcov --exclude \"\" && xdg-open coverage/lcov-report/index.html"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/prettier": "^2.7.0",
"@types/slug": "^5.0.9",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
"assert": "^2.0.0",
"eslint": "^8.38.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-with-tsc-error": "^0.0.8",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
},
"types": "./@types/index.d.ts",
"dependencies": {
"@sealcode/file-manager": "^1.0.2",
"@sealcode/ts-predicates": "^0.5.3",
"escape-goat": "^4.0.0",
"html-entities": "^2.5.2",
"hyphenopoly": "^5.3.0",
"koa-responsive-image-router": "^0.2.29",
"locreq": "^3.0.0",
"marked": "^12.0.0",
"mri": "^1.2.0",
"prettier": "^2.7.1",
"slug": "^9.1.0",
"tempstream": "^0.4.1",
"uuid": "^9.0.1"
},
"peerDependencies": {
"sealious": "^0.19.29"
}
}
diff --git a/src/component-arguments/single-reference.ts b/src/component-arguments/single-reference.ts
index 425daa3..07dabb6 100644
--- a/src/component-arguments/single-reference.ts
+++ b/src/component-arguments/single-reference.ts
@@ -1,87 +1,93 @@
import { is, predicates } from "@sealcode/ts-predicates";
import { JDDContext } from "../jdd-context.js";
import { MaybePromise } from "../utils/util-types.js";
import { StringBasedArgument } from "./string-based-argument.js";
import { Collection, CollectionItem, Context } from "sealious";
function pickRandom<T>(array: T[]): T {
return array[Math.floor(Math.random() * array.length)];
}
export class SingleReference<
C extends Collection
> extends StringBasedArgument<string> {
constructor(
public getSealiousCollection: () => Promise<C>,
public getSealiousContext: (jdd_context: JDDContext) => Context,
public itemToLabel: (item: CollectionItem<C>) => Promise<string>
) {
super();
}
getTypeName() {
return "single-reference";
}
async getEmptyValue(jdd_ctx: JDDContext) {
const collection = await this.getSealiousCollection();
const ids =
- (await jdd_ctx.ctx?.$cache(
+ (await jdd_ctx.ctx.$cache(
`jdd__single_reference__${collection.name}__ids`,
async () => {
const context = this.getSealiousContext(jdd_ctx);
const ids = (
await collection
.list(context)
.paginate({ items: 5 })
.fetch()
).items.map(({ id }) => id);
return ids;
}
)) || [];
if (!ids.length) {
return "";
} else {
return pickRandom(ids);
}
}
async getValues(
jdd_ctx: JDDContext
): Promise<{ label: string; value: string }[]> {
- const context = this.getSealiousContext(jdd_ctx);
- const { items } = await (await this.getSealiousCollection())
- .list(context)
- .fetch();
+ const collection = await this.getSealiousCollection();
+ return jdd_ctx.ctx.$cache(
+ `jdd__single_reference__${collection.name}__labels`,
+ async () => {
+ const context = this.getSealiousContext(jdd_ctx);
+ const { items } = await (await this.getSealiousCollection())
+ .list(context)
+ .fetch();
- const labels = Promise.all(
- items.map(async (item) => ({
- value: item.id,
- label: await this.itemToLabel(item),
- }))
+ const labels = Promise.all(
+ items.map(async (item) => ({
+ value: item.id,
+ label: await this.itemToLabel(item),
+ }))
+ );
+ return labels;
+ }
);
- return labels;
}
getExampleValue(context: JDDContext) {
return this.getEmptyValue(context);
}
countWords(): number {
return 0;
}
example_values = [];
parseFormInput(
_: JDDContext,
input: unknown
): MaybePromise<string | string[] | null> {
if (is(input, predicates.string)) {
return input;
} else {
return null;
}
}
}
diff --git a/src/jdd-context.ts b/src/jdd-context.ts
index 450b875..81bb7d7 100644
--- a/src/jdd-context.ts
+++ b/src/jdd-context.ts
@@ -1,86 +1,86 @@
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";
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;
+ ctx: Koa.Context;
}
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
) => Omit<JDDContext, "language"> = (file_manager) => ({
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);
},
render_markdown: async (language, string) => {
const hyphenator = await get_hyphenator(language);
return string
? marked.parse(insert_nbsp(string), {
walkTokens: (token: Token) => {
if (token.type == "text") {
token.text = hyphenator(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,
});
export const makeSimpleEnglishJDDContext: (
file_manager: FileManager
) => JDDContext = (file_manager) => ({
...makeSimpleJDDContext(file_manager),
language: "en-us",
});

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 24, 01:49 (21 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
548070
Default Alt Text
(7 KB)

Event Timeline