top level md filter

This commit is contained in:
lovebird 2025-06-04 12:41:14 +02:00
parent 86a695602f
commit 976dea2757
9 changed files with 82 additions and 36 deletions

File diff suppressed because one or more lines are too long

View File

@ -4,6 +4,8 @@ export declare const extractCodeBlocks: (markdown: string) => string;
export declare const JSONParse: (json: string) => any;
export declare const trim: (str: string) => string;
export declare const extractJsonCodeBlock: (markdown: string) => string | null;
export declare const markdown: (markdown: string) => string;
export declare const applyFilters: (value: string, filters: Filter[]) => string;
export declare const Filters: {
JSON: (markdown: string) => string | null;
JSONUnescape: (json: string) => string;
@ -12,5 +14,5 @@ export declare const Filters: {
code: (markdown: string) => string | null;
JSONParse: (json: string) => any;
trim: (str: string) => string;
markdown: (markdown: string) => string;
};
export declare const applyFilters: (value: string, filters: Filter[]) => string;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -92,7 +92,7 @@ export const complete_options = async (opts: IKBotTask): Promise<IKBotTask | nul
return options
} catch (error) {
opts.logger.error('Failed to parse options:', error.message, error.issues)
opts.logger.error('Failed to parse options:', error.message, error.issues, error.stack)
return null
}
}

View File

@ -106,15 +106,32 @@ const JSONPretty = (json: string) => {
return JSON.stringify(JSON.parse(json), null, 2)
}
export const Filters = {
JSON: extractJsonCodeBlock,
JSONUnescape: unescapeJsonString,
JSONPretty,
AlphaSort: alphanumericSort,
code: extractFirstCodeBlock,
JSONParse,
trim
}
export const markdown = (markdown: string): string => {
const trimmedMarkdown = markdown.trim();
// This regex is anchored to match if the *entire* trimmed string is a code block.
// It's adapted from the regex in extractFirstCodeBlock.
// ^``` : Asserts the start of the string followed by ```.
// (\w+\n)? : Optional capturing group (Group 1) for a language specifier
// (e.g., "typescript\n"). \w+ requires one or more word characters
// for the language, followed by a newline. This group is optional.
// ([\s\S]*?) : Capturing group (Group 2) for the content of the code block.
// Matches any character (including newlines) in a non-greedy way.
// ```$ : Asserts ``` followed by the end of the string.
const regex = /^```(\w+\n)?([\s\S]*?)```$/;
const match = trimmedMarkdown.match(regex);
if (match && match[2] !== undefined) {
// match[2] contains the content of the code block.
// Consistent with extractFirstCodeBlock, we trim the resulting content.
return match[2].trim();
}
// If the regex didn't match, it means the string is not a single,
// top-level code block that spans the entire string.
return markdown;
};
export const applyFilters = (value: string, filters: Filter[]) => {
if (!value) {
@ -133,4 +150,15 @@ export const applyFilters = (value: string, filters: Filter[]) => {
}
})
return value
}
export const Filters = {
JSON: extractJsonCodeBlock,
JSONUnescape: unescapeJsonString,
JSONPretty,
AlphaSort: alphanumericSort,
code: extractFirstCodeBlock,
JSONParse,
trim,
markdown
}

View File

@ -1,6 +1,4 @@
import * as path from 'node:path'
import * as fs from 'node:fs'
import { sync as read } from '@polymech/fs/read'
import { sync as dir } from '@polymech/fs/dir'
import { createItem as toNode } from '@polymech/fs/inspect'

View File

@ -30,11 +30,9 @@ export const generateSingleFileVariables = (filePath: string, projectPath: strin
});
}
};
addIndexedParts('SRC_NAME-', srcParts.name, '-');
addIndexedParts('SRC_NAME.', srcParts.name, '.');
addIndexedParts('SRC_NAME_', srcParts.name, '_');
addIndexedParts('SRC_NAME_', srcParts.name, '_');
// Convert all keys to uppercase
const uppercasedVariables: Record<string, string> = {};
for (const key in fileSpecificVariables) {
@ -54,17 +52,14 @@ export const variables = (options: IKBotTask) => {
...DEFAULT_VARS({})
}
if (options?.include?.length === 1) {
const [include] = options.include
if (options.include && options.include[0]) {
const include = options.include[0]
const { } = pathInfoEx(include)
const srcParts = path.parse(include)
const srcVariables: Record<string, string> = {}
srcVariables.SRC_NAME = srcParts.name
srcVariables.SRC_DIR = srcParts.dir
srcVariables.SRC_EXT = srcParts.ext
if (srcVariables.ROOT) {
srcVariables.SRC_REL = path.relative(srcVariables.ROOT, srcParts.dir)
}