Jump to content

Module:SocialMedia: Difference between revisions

mNo edit summary
accessibility
Line 1: Line 1:
-- Module:SocialMedia
-- Reusable, single-row or multi-row social media footer that can be called from any other module or template.
-- Reusable, single-row or multi-row social media footer that can be called from any other module or template.
local sf = {}
local sf = {}


-- 1) Define the social platforms
-- 1) Define the social platforms
--    param  = the name of the parameter
--    param  = the name of the parameter (|param= in the wikitext)
--    icon    = wiki file for the icon
--    icon    = wiki file for the icon
--    prefix  = URL prefix
--    prefix  = URL prefix
--    label  = optional for referencing or debugging
--    label  = used here as the image alt text for accessibility
local socialPlatforms = {
local socialPlatforms = {
     {
     {
Line 59: Line 59:
}
}


-- 2) Helper function to build a clickable icon link
-- 2) Helper function to build a clickable icon link with alt text
--    Using wikitext syntax: [[File:Something.png|16px|link=URL]]
--    Using wikitext syntax: [[File:Something.png|16px|alt=ALT_TEXT|link=URL]]
local function buildIconLink(iconFile, url)
local function buildIconLink(iconFile, altText, url)
     return string.format("[[%s|16px|link=%s]]", iconFile, url)
     return string.format("[[%s|16px|alt=%s|link=%s]]", iconFile, altText, url)
end
end


-- 3) The main render function, called from another module or via #invoke
-- 3) The main render function, called from another module or via #invoke.
-- Takes a table of arguments (handles), returns wiki markup with social icons.
--   Takes a table of arguments (handles), returns wiki markup with social icons.
function sf.render(args)
function sf.render(args)
     local icons = {}
     local icons = {}
Line 75: Line 75:
             local url
             local url


             -- Special-case Mastodon, as it can vary. Example approach:
             -- Special-case Mastodon logic, if user hasn't provided a full URL
             if platform.param == "mastodon" then
             if platform.param == "mastodon" then
                 if handle:match("^https?://") then
                 if handle:match("^https?://") then
Line 91: Line 91:
             end
             end


             -- Build an icon link
             -- Build an icon link, passing `label` as the alt text
             local iconLink = buildIconLink(platform.icon, url)
             local iconLink = buildIconLink(platform.icon, platform.label, url)
             table.insert(icons, iconLink)
             table.insert(icons, iconLink)
         end
         end
Line 102: Line 102:
     end
     end


     -- Otherwise, return a table row or any layout you want.
     -- Otherwise, return a row or any layout you want.
     -- Example: single row labeled "Social Media"
     -- Example: single row labeled "Social Media"
     local iconString = table.concat(icons, "   ")
     local iconString = table.concat(icons, "   ")

Revision as of 12:37, 7 February 2025

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

-- Module:SocialMedia
-- Reusable, single-row or multi-row social media footer that can be called from any other module or template.
local sf = {}

-- 1) Define the social platforms
--    param   = the name of the parameter (|param= in the wikitext)
--    icon    = wiki file for the icon
--    prefix  = URL prefix
--    label   = used here as the image alt text for accessibility
local socialPlatforms = {
    {
        param  = "facebook",
        icon   = "File:FacebookIcon.png",
        prefix = "https://www.facebook.com/",
        label  = "Facebook"
    },
    {
        param  = "instagram",
        icon   = "File:InstagramIcon.png",
        prefix = "https://www.instagram.com/",
        label  = "Instagram"
    },
    {
        param  = "linkedin",
        icon   = "File:LinkedInIcon.png",
        prefix = "https://www.linkedin.com/",
        label  = "LinkedIn"
    },
    {
        param  = "mastodon",
        icon   = "File:MastodonIcon.png",
        prefix = "",
        label  = "Mastodon"
    },
    {
        param  = "threads",
        icon   = "File:ThreadsIcon.png",
        prefix = "https://www.threads.net/",
        label  = "Threads"
    },
    {
        param  = "tiktok",
        icon   = "File:TikTokIcon.png",
        prefix = "https://www.tiktok.com/",
        label  = "TikTok"
    },
    {
        param  = "twitter",
        icon   = "File:TwitterIcon.png",  -- or "File:XIcon.png"
        prefix = "https://x.com/",
        label  = "X (Twitter)"
    },
    {
        param  = "youtube",
        icon   = "File:YouTubeIcon.png",
        prefix = "https://www.youtube.com/",
        label  = "YouTube"
    },
}

-- 2) Helper function to build a clickable icon link with alt text
--    Using wikitext syntax: [[File:Something.png|16px|alt=ALT_TEXT|link=URL]]
local function buildIconLink(iconFile, altText, url)
    return string.format("[[%s|16px|alt=%s|link=%s]]", iconFile, altText, url)
end

-- 3) The main render function, called from another module or via #invoke.
--    Takes a table of arguments (handles), returns wiki markup with social icons.
function sf.render(args)
    local icons = {}

    for _, platform in ipairs(socialPlatforms) do
        local handle = args[platform.param]
        if handle and handle ~= "" then
            local url

            -- Special-case Mastodon logic, if user hasn't provided a full URL
            if platform.param == "mastodon" then
                if handle:match("^https?://") then
                    -- user typed a full URL
                    url = handle
                else
                    -- parse e.g. "icann@mastodon.social"
                    local domain = handle:match("@([^@]+)$") or "mastodon.social"
                    local user   = handle:match("^(.-)@") or handle
                    url = "https://" .. domain .. "/@" .. user
                end
            else
                -- For everything else, prefix + handle
                url = platform.prefix .. handle
            end

            -- Build an icon link, passing `label` as the alt text
            local iconLink = buildIconLink(platform.icon, platform.label, url)
            table.insert(icons, iconLink)
        end
    end

    -- If no handles were provided, return nothing (empty string).
    if #icons == 0 then
        return ""
    end

    -- Otherwise, return a row or any layout you want.
    -- Example: single row labeled "Social Media"
    local iconString = table.concat(icons, "   ")
    local row = table.concat({
        "|-",
        "| '''Social Media''':",  -- Label on the left
        "| " .. iconString        -- Icons on the right
    }, "\n")

    return row
end

return sf