Module:FeaturedSnippet

From The Global Wiki
Jump to navigation Jump to search

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

local p = {}

function p.render(frame)
	local out = {}
	local args = frame:getParent().args

	local snippets = {
		args["snippet"],
		args["snippet2"],
		args["snippet3"]
	}

	-- Add provided snippets
	for _, snippet in ipairs(snippets) do
		if snippet and snippet ~= "" then
			table.insert(out, '<p>' .. mw.text.encode(snippet) .. '</p>')
		end
	end

	-- Fallback to first paragraph if no snippets
	if #out == 0 then
		local content = mw.title.getCurrentTitle():getContent()
		if content then
			local firstPara = content:match("^(.-)\n")
			if firstPara and firstPara ~= "" then
				table.insert(out, '<p>' .. mw.text.encode(mw.text.trim(firstPara)) .. '</p>')
			end
		end
	end

	-- Add hidden keywords for SEO (non-HTML)
	local keywords = args["keywords"]
	if keywords and keywords ~= "" then
		table.insert(out, '<span style="display:none;">Keywords: ' .. mw.text.encode(keywords) .. '</span>')
	end

	return table.concat(out, "\n")
end

return p