export function upsertFrontmatterField(block, field, value, overwrite) {
    const matcher = new RegExp(`^${field}:\\s*([^\\n]*?)(\\s*#.*)?$`, 'm');
    const match = block.match(matcher);
    if (match) {
        if (!overwrite) {
            return { nextBlock: block, changed: false, added: false };
        }
        const comment = match[2] ?? '';
        const nextBlock = block.replace(matcher, `${field}: ${value}${comment}`);
        return { nextBlock, changed: nextBlock !== block, added: false };
    }
    const normalized = block.endsWith('\n') ? block : `${block}\n`;
    return {
        nextBlock: `${normalized}${field}: ${value}`,
        changed: true,
        added: true,
    };
}
export function replaceFrontmatter(content, nextBlock) {
    return content.replace(/^---\n([\s\S]*?)\n---/, `---\n${nextBlock}\n---`);
}
export function removeFrontmatterField(block, field) {
    return block
        .split('\n')
        .filter((line) => !line.startsWith(`${field}:`))
        .join('\n');
}
