-- subsection-wrapper.lua
-- Pandoc filter: indent everything 4% right, then pull H1s back with a -4% margin, ONLY if an H1 exists

function Pandoc(doc)
    local has_h1 = false
    for _, block in ipairs(doc.blocks) do
        if block.t == "Header" and block.level == 1 then
            has_h1 = true
            break
        end
    end

    if not has_h1 then
        return doc
    end

    local new_blocks = {}
    for _, block in ipairs(doc.blocks) do
        if block.t == "Header" and block.level == 1 then
            -- Wrap H1 in a div that cancels out the parent indent
            local h1_div = pandoc.Div({block}, {style = "margin-left: -4%;"})
            table.insert(new_blocks, h1_div)
        else
            table.insert(new_blocks, block)
        end
    end

    -- Wrap everything in a single div shifted right by 4%
    local outer = pandoc.Div(new_blocks, {style = "margin-left: 4%;"})
    return pandoc.Pandoc({outer}, doc.meta)
end
