God Mode Valhalla: Initial Standalone Commit
This commit is contained in:
44
src/lib/assembler/data.ts
Normal file
44
src/lib/assembler/data.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
import { directus } from '@/lib/directus/client';
|
||||
import { readItems } from '@directus/sdk';
|
||||
|
||||
/**
|
||||
* Fetches all spintax dictionaries and flattens them into a usable SpintaxMap.
|
||||
* Returns: { "adjective": "{great|good|awesome}", "noun": "{cat|dog}" }
|
||||
*/
|
||||
export async function fetchSpintaxMap(): Promise<Record<string, string>> {
|
||||
try {
|
||||
const items = await directus.request(
|
||||
readItems('spintax_dictionaries', {
|
||||
fields: ['category', 'variations'],
|
||||
limit: -1
|
||||
})
|
||||
);
|
||||
|
||||
const map: Record<string, string> = {};
|
||||
|
||||
items.forEach((item: any) => {
|
||||
if (item.category && item.variations) {
|
||||
// Example: category="premium", variations="{high-end|luxury|top-tier}"
|
||||
map[item.category] = item.variations;
|
||||
}
|
||||
});
|
||||
|
||||
return map;
|
||||
} catch (error) {
|
||||
console.error('Error fetching spintax:', error);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a new pattern (template) to the database.
|
||||
*/
|
||||
export async function savePattern(patternName: string, structure: string) {
|
||||
// Assuming 'cartesian_patterns' is where we store templates
|
||||
// or we might need a dedicated 'templates' collection if structure differs.
|
||||
// For now using 'cartesian_patterns' as per config.
|
||||
|
||||
// Implementation pending generic createItem helper or direct SDK usage
|
||||
// This will be called by the API endpoint.
|
||||
}
|
||||
68
src/lib/assembler/engine.ts
Normal file
68
src/lib/assembler/engine.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
|
||||
/**
|
||||
* Spintax Processing Engine
|
||||
* Handles nested spintax formats: {option1|option2|{nested1|nested2}}
|
||||
*/
|
||||
|
||||
export function processSpintax(text: string): string {
|
||||
if (!text) return '';
|
||||
|
||||
// Regex to find the innermost spintax group { ... }
|
||||
const spintaxRegex = /\{([^{}]*)\}/;
|
||||
|
||||
let processedText = text;
|
||||
let match = spintaxRegex.exec(processedText);
|
||||
|
||||
// Keep processing until no more spintax groups are found
|
||||
while (match) {
|
||||
const fullMatch = match[0]; // e.g., "{option1|option2}"
|
||||
const content = match[1]; // e.g., "option1|option2"
|
||||
|
||||
const options = content.split('|');
|
||||
const randomOption = options[Math.floor(Math.random() * options.length)];
|
||||
|
||||
processedText = processedText.replace(fullMatch, randomOption);
|
||||
|
||||
// Re-check for remaining matches (including newly exposed or remaining groups)
|
||||
match = spintaxRegex.exec(processedText);
|
||||
}
|
||||
|
||||
return processedText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Variable Substitution Engine
|
||||
* Replaces {{variable_name}} with provided values.
|
||||
* Supports fallback values: {{variable_name|default_value}}
|
||||
*/
|
||||
export function processVariables(text: string, variables: Record<string, string>): string {
|
||||
if (!text) return '';
|
||||
|
||||
return text.replace(/\{\{([^}]+)\}\}/g, (match, variableKey) => {
|
||||
// Check for default value syntax: {{city|New York}}
|
||||
const [key, defaultValue] = variableKey.split('|');
|
||||
|
||||
const cleanKey = key.trim();
|
||||
const value = variables[cleanKey];
|
||||
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
return value;
|
||||
}
|
||||
|
||||
return defaultValue ? defaultValue.trim() : match; // Return original if no match and no default
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Master Assembly Function
|
||||
* Runs spintax first, then variable substitution.
|
||||
*/
|
||||
export function assembleContent(template: string, variables: Record<string, string>): string {
|
||||
// 1. Process Spintax (Randomize structure)
|
||||
const spunContent = processSpintax(template);
|
||||
|
||||
// 2. Substitute Variables (Inject specific data)
|
||||
const finalContent = processVariables(spunContent, variables);
|
||||
|
||||
return finalContent;
|
||||
}
|
||||
0
src/lib/assembler/quality.ts
Normal file
0
src/lib/assembler/quality.ts
Normal file
0
src/lib/assembler/seo.ts
Normal file
0
src/lib/assembler/seo.ts
Normal file
0
src/lib/assembler/spintax.ts
Normal file
0
src/lib/assembler/spintax.ts
Normal file
0
src/lib/assembler/variables.ts
Normal file
0
src/lib/assembler/variables.ts
Normal file
Reference in New Issue
Block a user