Module:GetFirstImage

From IxWiki
Revision as of 13:31, 30 July 2024 by Heku (talk | contribs) (Created page with "local p = {} function p.getFirstImage(frame) local pageName = frame.args[1] if not pageName then return '' end local page = mw.title.new(pageName) if not page then return '' end local content = page:getContent() if not content then return '' end -- Look for the first [[File: or [[Image: in the content local fileName = content:match("%[%[[Ff]ile:([^%]|]+)") or content:match("%[%[[Ii]mage:([^%]|]+)") if fileName then...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:GetFirstImage/doc

local p = {}

function p.getFirstImage(frame)
    local pageName = frame.args[1]
    if not pageName then return '' end
    
    local page = mw.title.new(pageName)
    if not page then return '' end
    
    local content = page:getContent()
    if not content then return '' end
    
    -- Look for the first [[File: or [[Image: in the content
    local fileName = content:match("%[%[[Ff]ile:([^%]|]+)") or content:match("%[%[[Ii]mage:([^%]|]+)")
    
    if fileName then
        return fileName
    else
        return 'Placeholderv2.png'  -- Default image if no image is found
    end
end

return p