Module:SEO utilities

From Fallen London Wiki

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

local p = {}

-- Removes extraneous wikitext from game text used for a SEO description
local function strip_game_wikitext(text)
	-- Remove wikitext bold/italics
	text = mw.ustring.gsub(text, "'''?", '')
	-- Remove span tags
	text = mw.ustring.gsub(text, '</?span[^>]->', '')
	-- Remove categories
	text = mw.ustring.gsub(text, '%[%[Category:.-%]%]', '')
	-- Remove inline images, usually from {{IL}} and such
	text = mw.ustring.gsub(text, '%[%[File:.-%]%] ?', '')
	-- Remove wikitext from no-label links
	text = mw.ustring.gsub(text, '%[%[([^%]|]-)%]%]', '%1')
	-- Replace labelled wikitext links with the label text
	text = mw.ustring.gsub(text, '%[%[[^%]|]-|([^|]-)%]%]', '%1')
	return text
end

function p.strip_game_wikitext(frame)
	local text = frame.args.text
	return frame:preprocess(strip_game_wikitext(text))
end

-- Generate and set SEO description from branch description
function p.SEO_description(frame)
	local description = mw.text.trim(frame.args.description or '')
	local game_instructions = mw.text.trim(frame.args.game_instructions or '')
	local truncate = frame.args.truncate or 'yes'
	-- TODO if {{{Description}}} is empty, extract a reasonable description from {{{Description summmary}}}
	
	local seo_desc
	if description ~= '' then
		if truncate == 'no' then
			seo_desc = description
		else
			seo_desc = frame:expandTemplate{ title = 'Truncate', args = { description, Warning = 'no' } }
		end
	elseif game_instructions ~= '' then
		seo_desc = game_instructions
	else
		seo_desc = '[Find the full story at https://www.fallenlondon.com]'
	end

	seo_desc = strip_game_wikitext(seo_desc)
	-- TODO test that Noembed works when preprocessed by the module
	return frame:preprocess('{{Noembed|{{#seo:description=' .. seo_desc .. '}}}}')
end

return p