kbot filters, i am so excited :)

This commit is contained in:
lovebird 2025-03-26 20:51:05 +01:00
parent b3da60538c
commit bfe361c704
5 changed files with 35 additions and 27 deletions

View File

@ -23,7 +23,7 @@
"format": "unix-time"
}
],
"default": "2025-03-26T15:56:17.684Z"
"default": "2025-03-26T19:46:20.703Z"
},
"description": {
"type": "string",

File diff suppressed because one or more lines are too long

View File

@ -16,6 +16,7 @@ export interface Props extends TemplateProps {
}
export const filter = async (content: string, tpl: string = 'howto', opts: Props = {}) => {
if (!content || content.length < 20) {
return content;
}

View File

@ -8,9 +8,7 @@ interface Item {
};
}
interface FilterFunction {
(text: string): string | Promise<string>;
}
export interface FilterFunction { (text: string): string | Promise<string> }
// Constants
export const item_path = (item: Item): string => `${HOWTO_ROOT()}/${item.data.slug}`;
@ -96,8 +94,7 @@ export const replaceWords = (text: string): string =>
text
);
// Filter pipeline
export const filters: readonly FilterFunction[] = [
export const default_filters: FilterFunction[] = [
renderLinks,
filterBannedPhrases,
replaceWords
@ -108,15 +105,13 @@ export const filters: readonly FilterFunction[] = [
* @param text - The text to filter
* @returns Promise resolving to the filtered text
*/
export async function applyFilters(text: string): Promise<string> {
if (!text) return '';
export async function applyFilters(text: string = '',filters: FilterFunction[] = default_filters): Promise<string> {
return filters.reduce(
async (promise, filterFn) => {
const currentText = await promise;
return filterFn(currentText);
},
Promise.resolve(text)
);
)
}

View File

@ -12,12 +12,15 @@ import type { Loader, LoaderContext } from 'astro/loaders'
export * from './howto-model.js'
export * from './filters.js'
import { filter as language } from "@/base/kbot.js"
import { IHowto, IImage, ITag, ITEM_TYPE } from './howto-model.js'
import type { IAnnotation } from "./annotation.js"
import { AnnotationMode, generateCacheKey, cacheAnnotation, getCachedAnnotation } from './annotation.js'
import { blacklist } from './filters.js'
import { download } from './download.js'
import { filter } from "@/base/kbot.js"
import type { IAnnotation } from "./annotation.js"
import { AnnotationMode, generateCacheKey, cacheAnnotation, getCachedAnnotation } from './annotation.js'
import {
HOWTO_FILES_WEB,
HOWTO_FILES_ABS,
@ -28,10 +31,12 @@ import {
HOWTO_GLOB,
HOWTO_MIGRATION,
HOWTO_ANNOTATIONS
} from "config/config.js";
} from "config/config.js"
import { logger } from '@/base/index.js'
import { applyFilters } from './filters.js'
import { applyFilters, default_filters, FilterFunction } from './filters.js'
import { TemplateContext } from '@/base/kbot-templates.js';
//export const load = () => get(`${HOWTO_ROOT()}/${HOWTO_GLOB}`, HOWTO_ROOT(), ITEM_TYPE)
export const item_path = (item: any) => `${HOWTO_ROOT()}/${item.data.slug}`
@ -146,24 +151,29 @@ export const defaults = async (data: any, cwd: string, root: string) => {
return data;
}
const content = async (str: string) => {
const ret = await applyFilters(str)
return ret;
};
const content = async (str: string, filters: FilterFunction[] = default_filters) => await applyFilters(str, filters)
const complete = async (item: IHowto) =>
{
if(!HOWTO_ANNOTATIONS){
const complete = async (item: IHowto) => {
if (!HOWTO_ANNOTATIONS) {
return item
}
const stepsWithFilteredMarkdown = await pMap(
// default pass, links, words, formatting, ...
item.steps = await pMap(
item.steps,
async (step) => ({
...step,
text: await content(step.text)
}),
{ concurrency: 1 },
);
})
)
if (HOWTO_FILTER_LLM) {
item.steps = await pMap(
item.steps,
async (step) => ({
...step,
text: await content(step.text, [(text) => filter(text, 'howto')])
})
)
}
return item
}
@ -171,6 +181,7 @@ const onStoreItem = async (store: any, ctx: LoaderContext) => {
const item = store.data.item as IHowto
item.steps = item.steps || []
item.cover_image && (item.cover_image.src = await asset_local_rel(item, item.cover_image))
item.steps = await pMap(item.steps, async (step) => {
step.images = await pMap(step.images, async (image) => {
return {
@ -183,6 +194,7 @@ const onStoreItem = async (store: any, ctx: LoaderContext) => {
});
return step;
}, { concurrency: 1 })
item.steps.forEach((step) => {
step.images = step.images.filter((image) => asset_local_abs(item, image))
})