Module:WikitextProcessor: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Tag: Reverted
Line 22: Line 22:
     end
     end
     return fallbackValue
     return fallbackValue
end
-- Interwiki minimal allowlist (test)
local INTERWIKI = {
    w = 'https://en.wikipedia.org/wiki/$1',
    wikipedia = 'https://en.wikipedia.org/wiki/$1',
}
local function resolveInterwiki(pageName)
    if type(pageName) ~= "string" then return nil end
    local prefix, title = pageName:match('^([^:]+):(.*)$')
    if not prefix or not title or prefix == '' or title == '' then return nil end
    local tpl = INTERWIKI[prefix:lower()]
    if not tpl then return nil end
    local spacesReplaced = (title:gsub(' ', '_'))
    return tpl:gsub('%$1', spacesReplaced)
end
end


Line 30: Line 46:
         pattern = '%[%[([^#|%]]+)%]%]',
         pattern = '%[%[([^#|%]]+)%]%]',
         processor = function(pageName, errorContext)
         processor = function(pageName, errorContext)
            -- Interwiki handling (minimal allowlist)
            local iw = resolveInterwiki(pageName)
            if iw then
                local pageNameStr = type(pageName) == "string" and pageName or tostring(pageName)
                local ok
                ok, pageNameStr = pcall(mw.text.encode, pageNameStr)
                if not ok then
                    return handleError(errorContext, 'encodeFailed', '[[' .. pageName .. ']]')
                end
                return string.format('<a href="%s" class="extiw">%s</a>', iw, pageNameStr)
            end
             local spacesReplaced = (pageName:gsub(' ', '_'))
             local spacesReplaced = (pageName:gsub(' ', '_'))
             local success, pageUrl = pcall(function()
             local success, pageUrl = pcall(function()
Line 57: Line 85:
         pattern = '%[%[([^#|%]]+)|([^%]]+)%]%]',
         pattern = '%[%[([^#|%]]+)|([^%]]+)%]%]',
         processor = function(pageName, text, errorContext)
         processor = function(pageName, text, errorContext)
            -- Interwiki handling (minimal allowlist)
            local iw = resolveInterwiki(pageName)
            if iw then
                local textStr = type(text) == "string" and text or tostring(text)
                local ok
                ok, textStr = pcall(mw.text.encode, textStr)
                if not ok then
                    return handleError(errorContext, 'encodeFailed', '[[' .. pageName .. '|' .. text .. ']]')
                end
                return string.format('<a href="%s" class="extiw">%s</a>', iw, textStr)
            end
             local success, pageUrl = pcall(function()
             local success, pageUrl = pcall(function()
                 return tostring(mw.uri.fullUrl((pageName:gsub(' ', '_'))))
                 return tostring(mw.uri.fullUrl((pageName:gsub(' ', '_'))))