Overview

let current = dv.current();
let title = current.name || current.file.name;
 
// --- CONFIGURATION ---
// Set to your silhouette image path or filename (e.g., "npc-images/silhouette.webp" or "silhouette.webp").
// If the file doesn't exist or is set to "", it will gracefully display the text message below.
let fallbackImage = "npc-images/silhouette.webp"; 
// ---------------------
 
// 1. Gather all potential names (Title, quoted nicknames, and Obsidian Aliases)
let rawNames = [title];
 
// Extract quoted nickname if present in title (e.g., Wulfric "The Bear" -> The Bear)
let matchNick = title.match(/"([^"]+)"/);
if (matchNick && matchNick) {
    rawNames.push(matchNick);
}
 
// Safely pull aliases from Obsidian Frontmatter/Properties
let rawAliases = current.file?.aliases || current.aliases || [];
if (typeof rawAliases === "string") {
    rawNames.push(rawAliases);
} else if (rawAliases && typeof rawAliases[Symbol.iterator] === "function") {
    for (let a of rawAliases) {
        if (a && typeof a === "string") rawNames.push(a);
    }
}
 
// 2. Build sanitized search terms (ensures terms are >= 2 chars to avoid matching "_" or empty strings)
let searchTerms = new Set();
for (let name of rawNames) {
    if (!name || typeof name !== "string") continue;
    
    let lower = name.toLowerCase().trim().replace(/["']/g, "");
    let slug = lower.replace(/[\s-]+/g, "_").replace(/^_+|_+\$/g, "");
    
    if (lower.length >= 2) searchTerms.add(lower);
    if (slug.length >= 2) searchTerms.add(slug);
}
 
// 3. Search npc-images for matching character files
let matches = app.vault.getFiles().filter(f => {
    let pathLower = f.path.toLowerCase();
    if (!pathLower.includes("npc-images")) return false;
    
    // Exclude the fallback image from character search
    if (fallbackImage && pathLower.includes(fallbackImage.toLowerCase())) return false;
    
    let imgName = f.name.toLowerCase();
    return Array.from(searchTerms).some(term => term && imgName.includes(term));
});
 
// 4. Prioritize files with "avatar" in the name, otherwise take first match
let match = matches.find(f => f.name.toLowerCase().includes("avatar")) || (matches.length > 0 ? matches : null);
 
// 5. Render artwork, fallback image, or clean text message
if (match) {
    dv.paragraph(`![[${match.path}|cover]]`);
} else {
    // Flexible fallback image lookup (works with filename, full path, or partial match)
    let fallbackFile = null;
    if (fallbackImage && fallbackImage.trim() !== "") {
        let cleanFB = fallbackImage.toLowerCase().trim();
        fallbackFile = app.vault.getFiles().find(f => 
            f.path.toLowerCase() === cleanFB || 
            f.path.toLowerCase().includes(cleanFB) || 
            f.name.toLowerCase().includes(cleanFB)
        );
    }
    
    if (fallbackFile) {
        dv.paragraph(`![[${fallbackFile.path}|cover]]`);
    } else {
        dv.paragraph(`*No artwork generated yet for ${title}*`);
    }
}

// 5. Render artwork or fallback if (match) { dv.paragraph(![[${match.path}|cover]]); } else { let fallbackFile = fallbackImage ? app.vault.getAbstractFileByPath(fallbackImage) : null; if (fallbackFile) { dv.paragraph(![[${fallbackFile.path}|cover]]); } else { dv.paragraph(*No artwork generated yet for ${title}*); } }

### Character Bio
Grak Stonebreaker, a seasoned veteran of numerous conflicts, is a respected half-orc warrior who serves as an officer and enforcer alongside the enigmatic sorcerer, [[Leander Stormrider]], under Lord [[Gideon von Scholtz]]. With his grey hair and imposing presence, Grak commands attention and instils both fear and respect among those who serve under him.

Known for his shrewdness and reserved demeanour outside of the battlefield, Grak possesses a wealth of experience that has honed his tactical skills to a razor’s edge. He has a keen understanding of military strategy and excels at analysing complex situations to determine the most effective course of action. As a result, he has earned a reputation as a skilled tactician, capable of turning the tide of battle in Lord Von Scholtz’s favour.

Under Grak’s command, his men operate with discipline and precision, knowing that any failure to adhere to his orders can have severe consequences. While his authoritative nature may strike fear into the hearts of his subordinates, there is also a deep sense of respect for his leadership. Grak leads by example, always displaying unwavering determination and fearlessness in the face of adversity. This earns him the loyalty and admiration of those who fight alongside him.

Off the battlefield, Grak remains reserved, speaking only when necessary. He values the importance of silence and observation, carefully considering his words before imparting them to others. This measured approach allows him to gauge the intentions and motivations of those around him, providing valuable insights that contribute to the success of his strategies.

Grak’s impressive track record and reputation as a skilled warrior have earned him the trust and confidence of Lord Von Scholtz. Together with Leander Stormrider, the sorcerer, they form a formidable duo, combining magical finesse and tactical prowess. Their collaboration ensures that Lord Von Scholtz’s commands are executed flawlessly and his enemies are met with fierce resistance.

In the realm of conflict and leadership, Grak Stonebreaker’s name carries weight. His years of experience, strategic acumen, and unwavering determination make him an invaluable asset to Lord Gideon von Scholtz and a force to be reckoned with on the battlefield.

Appearance & Personality