Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

16.8 KB
Raw Blame
/**
 * Utility functions for formatting data
 */

// Canonical inner-target pattern for a [[wiki-link]] — the text between the
// brackets. A wiki-link target must NOT span a newline (it mirrors how the
// backend link parser resolves [[Title]]), so the class excludes both `]`
// and `\n`. This is the single source of truth: note-detail.js
// (processWikiLinks and renderNoteMarkdown) reuses it via
// window.formatting.WIKILINK_TARGET so parse and render agree on newline
// handling. Exported as a source string (not a RegExp) so each call site can
// build its own stateful global regex without sharing lastIndex.
const WIKILINK_TARGET = '[^\\]\\n]+';

/**
 * Format a status string to be more user-friendly
 * @param {string} status - The status string
 * @returns {string} The formatted status string
 */
function formatStatus(status) {
    return ResearchStates.formatStatus(status);
}

/**
 * Format a research mode string to be more user-friendly
 * @param {string} mode - The mode string
 * @returns {string} The formatted mode string
 */
function formatMode(mode) {
    switch(mode) {
        case 'quick': return 'Quick Summary';
        case 'detailed': return 'Detailed Report';
        default: return mode.charAt(0).toUpperCase() + mode.slice(1);
    }
}

/**
 * Format a date string with optional duration
 * @param {string} date - The date string in ISO format
 * @param {number|null} duration - Optional duration in seconds
 * @returns {string} The formatted date string
 */
function formatDate(date, duration = null) {
    if (!date) return 'Unknown';

    try {
        const dateObj = new Date(date);
        const options = {
            year: 'numeric',
            month: 'short',
            day: 'numeric',
            hour: '2-digit',
            minute: '2-digit'
        };

        let formattedDate = dateObj.toLocaleDateString('en-US', options);

        if (duration) {
            // Format the duration
            const minutes = Math.floor(duration / 60);
            const seconds = duration % 60;

            if (minutes > 0) {
                formattedDate += ` (${minutes}m ${seconds}s)`;
            } else {
                formattedDate += ` (${seconds}s)`;
            }
        }

        return formattedDate;
    } catch (e) {
        SafeLogger.error('Error formatting date:', e);
        return date; // Return the original date if there's an error
    }
}

/**
 * Format a duration in seconds to a readable string
 * @param {number} seconds - Duration in seconds
 * @returns {string} Formatted duration string
 */
function formatDuration(seconds) {
    if (!seconds || isNaN(seconds)) return 'Unknown';

    const minutes = Math.floor(seconds / 60);
    const remainingSeconds = Math.floor(seconds % 60);

    if (minutes === 0) {
        return `${remainingSeconds}s`;
    }
    return `${minutes}m ${remainingSeconds}s`;
}

/**
 * Capitalize the first letter of a string
 * @param {string} string - The string to capitalize
 * @returns {string} The capitalized string
 */
function capitalizeFirstLetter(string) {
    if (!string) return '';
    return string.charAt(0).toUpperCase() + string.slice(1);
}

/**
 * Format a number with thousands separators
 * @param {number} num - The number to format
 * @returns {string} Formatted number
 */
function formatNumber(num) {
    // Metrics arrive as JSON, so never pass a string through to an HTML sink.
    const numeric = typeof num === 'number' || typeof num === 'string'
        ? Number(num)
        : NaN;
    if (!Number.isFinite(numeric)) return '0';
    return numeric.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}

/**
 * Format a dollar amount, using more decimal places for small numbers.
 * @param {number} amount - Amount in dollars
 * @returns {string} Formatted currency string (e.g. "$0.000123", "$12.34")
 */
function formatCurrency(amount) {
    if (amount < 0.01) {
        return `$${amount.toFixed(6)}`;
    } else if (amount < 1) {
        return `$${amount.toFixed(4)}`;
    }
    return `$${amount.toFixed(2)}`;
}

/**
 * Generate background/border colors for a chart with `count` categories,
 * cycling through a fixed base palette.
 * @param {number} count - Number of colors needed
 * @returns {{background: string[], border: string[]}}
 */
function generateChartColors(count) {
    const baseColors = [
        'rgba(107, 70, 193, 0.8)',   // Purple
        'rgba(245, 158, 11, 0.8)',   // Orange
        'rgba(59, 130, 246, 0.8)',   // Blue
        'rgba(16, 185, 129, 0.8)',   // Green
        'rgba(239, 68, 68, 0.8)',    // Red
        'rgba(139, 69, 19, 0.8)',    // Brown
        'rgba(255, 192, 203, 0.8)',  // Pink
        'rgba(128, 128, 128, 0.8)',  // Gray
        'rgba(255, 165, 0, 0.8)',    // Orange
        'rgba(75, 0, 130, 0.8)'      // Indigo
    ];

    const background = [];
    const border = [];

    for (let i = 0; i < count; i++) {
        const colorIndex = i % baseColors.length;
        background.push(baseColors[colorIndex]);
        border.push(baseColors[colorIndex].replace('0.8', '1'));
    }

    return { background, border };
}

/**
 * Basic markdown rendering (shared between notes pages).
 * Does NOT include [[wiki-link]] rendering — callers that need it
 * should post-process the returned HTML.
 * @param {string} text - Raw markdown text
 * @returns {string} HTML string
 */
// escapeHtml is only guaranteed as window.escapeHtml (see xss-protection.js / ui.js).
// Resolve it at CALL time, not module-load time: formatting.js is loaded (defer)
// before ui.js/xss-protection.js, so a module-load capture would always bind the
// fallback. The local fallback covers the same characters and is used only if the
// global isn't present.
function _escapeHtml(str) {
    if (typeof window !== 'undefined' && typeof window.escapeHtml === 'function') {
        return window.escapeHtml(str);
    }
    return String(str == null ? '' : str).replace(/[&<>"']/g, (m) => ({
        '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;',
    }[m]));
}

// Validate a markdown-link URL for safe rendering. A literal scheme blocklist is
// bypassable via embedded tab/newline/control chars (browsers strip them before
// resolving the scheme), so route through the URL parser which normalizes them.
function _isSafeLinkUrl(url) {
    if (typeof URLValidator !== 'undefined' && URLValidator.isSafeUrl) {
        return URLValidator.isSafeUrl(url, { allowMailto: true });
    }
    // Fallback: strip C0 controls + space, then let the URL parser decide the scheme.
    // eslint-disable-next-line no-control-regex -- stripping control chars is the point
    const cleaned = String(url == null ? '' : url).replace(/[\u0000-\u0020]/g, '');
    if (cleaned.startsWith('#')) return true;
    try {
        const parsed = new URL(cleaned, window.location.href);
        return ['http:', 'https:', 'ftp:', 'ftps:', 'mailto:'].includes(parsed.protocol);
    } catch (_e) {
        return false;
    }
}

// Bold / italic / strikethrough passes, shared between the main body and
// markdown-link labels (labels are emphasized separately because the link
// construct as a whole is tokenized before the body-wide emphasis pass).
// Input MUST already be HTML-escaped.
function _applyEmphasis(escaped) {
    return escaped
        .replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
        .replace(/__(.+?)__/g, '<strong>$1</strong>')
        .replace(/\*(.+?)\*/g, '<em>$1</em>')
        .replace(/_(.+?)_/g, '<em>$1</em>')
        .replace(/~~(.+?)~~/g, '<del>$1</del>');
}

function basicMarkdownRender(text) {
    if (!text) return '<p style="color: var(--text-secondary); font-style: italic;">Nothing to preview</p>';

    // Escape HTML first
    let html = _escapeHtml(text);

    // Headers
    html = html.replace(/^### (.+)$/gm, '<h3>$1</h3>');
    html = html.replace(/^## (.+)$/gm, '<h2>$1</h2>');
    html = html.replace(/^# (.+)$/gm, '<h1>$1</h1>');

    // Link constructs are extracted into placeholder tokens BEFORE the
    // emphasis passes, so underscores/asterisks inside URLs or [[wiki-link]]
    // targets are not rewritten into <em>/<strong> (which would corrupt
    // hrefs and break the callers' wiki-link text-node post-processing).
    // The token delimiters contain '<', which cannot appear literally in the
    // escaped input, so note content cannot forge a token.
    const tokens = [];
    const stash = (s) => `<!--LDRTOK${tokens.push(s) - 1}-->`;

    // Inline code has the highest inline precedence: its content is literal.
    // Stash it FIRST — before links, wiki-links and the emphasis pass — so a
    // `snake_case` / `a*b*c` span is not chopped into <em>/<strong> and a
    // `[label](url)` inside backticks does not become a live anchor. The
    // captured text is already HTML-escaped by the escape-first pass above.
    html = html.replace(/`([^`]+)`/g, (_m, code) =>
        stash(`<code style="background: var(--bg-tertiary); padding: 0.15rem 0.3rem; border-radius: 4px;">${code}</code>`));

    // [[wiki-links]] — kept verbatim for callers to post-process
    // (same target pattern as note-detail.js processWikiLinks).
    html = html.replace(new RegExp('\\[\\[' + WIKILINK_TARGET + '\\]\\]', 'g'), (match) => stash(match));

    // Links — only emit an anchor for URLs the URL parser confirms are safe.
    // (A literal scheme blocklist is bypassable via embedded control chars.)
    // Unsafe links stay as literal text; `match` is already entity-escaped
    // from the escape-first pass, so it renders inertly — re-escaping it
    // would display double-escaped entities (&amp;amp;) to the user.
    html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (match, label, url) => {
        const trimmed = url.trim();
        if (!_isSafeLinkUrl(trimmed)) {
            return stash(match);
        }
        return stash(`<a href="${trimmed}" target="_blank" rel="noopener noreferrer" style="color: var(--accent-primary);">${_applyEmphasis(label)}</a>`);
    });

    // Bold, italic and strikethrough (code spans are already stashed above,
    // so their literal contents are protected from this pass).
    html = _applyEmphasis(html);

    // Blockquotes
    html = html.replace(/^&gt; (.+)$/gm, '<blockquote style="border-left: 3px solid var(--accent-primary); padding-left: 1rem; margin: 0.5rem 0; color: var(--text-secondary);">$1</blockquote>');

    // Lists (simple)
    html = html.replace(/^- (.+)$/gm, '<li>$1</li>');
    html = html.replace(/^\d+\. (.+)$/gm, '<li>$1</li>');

    // Paragraphs (double newlines)
    html = html.replace(/\n\n/g, '</p><p>');
    html = '<p>' + html + '</p>';

    // Single newlines to <br>
    html = html.replace(/\n/g, '<br>');

    // Restore the stashed link constructs. Tokens are unforgeable (a literal
    // '<' in note content is escaped above), so every index is valid. A stashed
    // value may itself contain an earlier token (a [[wiki-link]] inside a link
    // label), and String.replace does not rescan replacements — so repeat until
    // stable. Values only reference earlier-created tokens, never themselves,
    // so this terminates.
    let beforeRestore;
    do {
        beforeRestore = html;
        html = html.replace(/<!--LDRTOK(\d+)-->/g, (_m, i) => tokens[i]);
    } while (html !== beforeRestore);

    return html;
}

/**
 * Apply markdown formatting to selected text in a textarea (shared between notes pages).
 * @param {HTMLTextAreaElement} textarea
 * @param {string} format - format key (bold, italic, etc.)
 * @param {Object} [extraFormats] - additional format entries to merge
 */
function applyMarkdownFormat(textarea, format, extraFormats) {
    const start = textarea.selectionStart;
    const end = textarea.selectionEnd;
    const text = textarea.value;
    const selected = text.substring(start, end);

    const formats = {
        bold: { before: '**', after: '**', placeholder: 'bold text' },
        italic: { before: '_', after: '_', placeholder: 'italic text' },
        strikethrough: { before: '~~', after: '~~', placeholder: 'strikethrough' },
        heading: { before: '## ', after: '', placeholder: 'Heading', lineStart: true },
        quote: { before: '> ', after: '', placeholder: 'quote', lineStart: true },
        code: { before: '`', after: '`', placeholder: 'code' },
        bullet: { before: '- ', after: '', placeholder: 'list item', lineStart: true },
        numbered: { before: '1. ', after: '', placeholder: 'list item', lineStart: true },
        link: { before: '[', after: '](url)', placeholder: 'link text' },
        // Wiki-style note link. Lives in the base map (not just the
        // note-detail extraFormats) because the notes editor's toolbar
        // dispatches formats straight through window.formatting without
        // passing extraFormats — so a "notelink" button only resolves if
        // the format is known here. Mirrors note-detail.js's extraFormats.
        notelink: { before: '[[', after: ']]', placeholder: 'Note Title' },
    };

    // Merge any extra formats (e.g. notelink for the detail page)
    if (extraFormats) {
        Object.assign(formats, extraFormats);
    }

    const fmt = formats[format];
    if (!fmt) return;

    let newText, newCursorStart, newCursorEnd;
    const content = selected || fmt.placeholder;

    if (fmt.lineStart) {
        const lineStart = text.lastIndexOf('\n', start - 1) + 1;
        newText = text.slice(0, lineStart) + fmt.before + text.slice(lineStart);
        newCursorStart = start + fmt.before.length;
        newCursorEnd = end + fmt.before.length;
    } else {
        newText = text.slice(0, start) + fmt.before + content + fmt.after + text.slice(end);
        if (selected) {
            newCursorStart = start + fmt.before.length;
            newCursorEnd = start + fmt.before.length + selected.length;
        } else {
            newCursorStart = start + fmt.before.length;
            newCursorEnd = start + fmt.before.length + fmt.placeholder.length;
        }
    }

    textarea.value = newText;
    textarea.focus();
    textarea.setSelectionRange(newCursorStart, newCursorEnd);
}

/**
 * Reduce markdown source to plain text for list/card previews.
 *
 * Single home for the note-preview strip shared by pages/notes.js and the
 * research/document notes components (previously three near-identical
 * copies that drifted — notes.js carried the superset, the components a
 * subset missing fenced-code / horizontal-rule / strikethrough handling).
 * The output is always assigned via textContent / escapeHtml'd and
 * rendered as TEXT, never HTML, so an imperfect strip degrades to stray
 * punctuation, never markup — the regex is intentionally lightweight.
 *
 * Exposed as a window.formatting METHOD, not a bare global function: the
 * callers keep their page-local wrapper names to avoid the deferred
 * shared-script global-shadowing bug; only the duplicated body is shared.
 */
function stripMarkdownToText(text) {
    if (!text) return '';
    return String(text)
        // Fenced-code markers (content inside is kept as plain text).
        .replace(/```[^\n]*/g, ' ')
        // Images before links: ![alt](url) → alt
        .replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1')
        // Links: [text](url) → text
        .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
        // Wiki links: [[Target|alias]] → alias, [[Target]] → Target
        .replace(/\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/g, (m, target, alias) => alias || target)
        // ATX headings + blockquote markers at line starts.
        .replace(/^\s{0,3}#{1,6}\s+/gm, '')
        .replace(/^\s{0,3}>\s?/gm, '')
        // List bullets / ordered-list markers at line starts.
        .replace(/^\s*(?:[-*+]|\d+\.)\s+/gm, '')
        // Horizontal rules on their own line.
        .replace(/^\s*(?:[-*_]\s*){3,}$/gm, ' ')
        // Emphasis / strikethrough / inline-code markers (keep the text).
        .replace(/(\*\*|__)(.*?)\1/g, '$2')
        .replace(/(\*|_)(.*?)\1/g, '$2')
        .replace(/~~(.*?)~~/g, '$1')
        .replace(/`([^`]*)`/g, '$1')
        // Collapse the leftover whitespace/newlines into single spaces.
        .replace(/\s+/g, ' ')
        .trim();
}

/**
 * Build the "% match" semantic-similarity badge shared by the notes list,
 * unified search, and the note-detail similar-notes/passages panels.
 * Returns '' for a null/undefined similarity so callers can drop it in
 * unconditionally. Previously inlined at 5 sites that had already drifted
 * (3 were missing the title tooltip the other 2 carried).
 */
function similarityBadge(similarity) {
    if (similarity === undefined || similarity === null) return '';
    return `<span class="ldr-similarity-badge" title="Semantic similarity">${Math.round(similarity * 100)}% match</span>`;
}

// Export the functions to make them available to other modules
window.formatting = {
    formatStatus,
    formatMode,
    formatDate,
    formatDuration,
    formatNumber,
    formatCurrency,
    generateChartColors,
    capitalizeFirstLetter,
    basicMarkdownRender,
    applyMarkdownFormat,
    stripMarkdownToText,
    similarityBadge,
    WIKILINK_TARGET
};