Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins/core): add config_octicon support #838

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@octokit/graphql": "^4.8.0",
"@octokit/rest": "^18.12.0",
"@primer/css": "^19.3.0",
"@primer/octicons": "^16.3.0",
"axios": "^0.25.0",
"clipboard": "^2.0.10",
"compression": "^1.7.4",
Expand Down
3 changes: 3 additions & 0 deletions source/app/metrics/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export default async function metrics({login, q}, {graphql, rest, plugins, conf,
style:extras.css,
twemojis:q["config.twemoji"],
gemojis:q["config.gemoji"],
octicons:q["config.octicon"],
rest,
})
}
Expand All @@ -172,6 +173,8 @@ export default async function metrics({login, q}, {graphql, rest, plugins, conf,
rendered = await imports.svg.twemojis(rendered)
if (q["config.gemoji"])
rendered = await imports.svg.gemojis(rendered, {rest})
if (q["config.octicon"])
rendered = await imports.svg.octicons(rendered)
//Optimize rendering
if ((conf.settings?.optimize === true) || (conf.settings?.optimize?.includes?.("css")))
rendered = await imports.svg.optimize.css(rendered)
Expand Down
26 changes: 24 additions & 2 deletions source/app/metrics/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import twemojis from "twemoji-parser"
import url from "url"
import util from "util"
import xmlformat from "xml-formatter"

import octicons from "@primer/octicons"
prism_lang()

//Exports
Expand Down Expand Up @@ -367,7 +367,7 @@ export async function imgb64(image, {width, height, fallback = true} = {}) {
/**SVG utils */
export const svg = {
/**Render as pdf */
async pdf(rendered, {paddings = "", style = "", twemojis = false, gemojis = false, rest = null} = {}) {
async pdf(rendered, {paddings = "", style = "", twemojis = false, gemojis = false, octicons = false, rest = null} = {}) {
//Instantiate browser if needed
if (!svg.resize.browser) {
svg.resize.browser = await puppeteer.launch()
Expand All @@ -378,6 +378,8 @@ export const svg = {
rendered = await svg.twemojis(rendered, {custom:false})
if ((gemojis) && (rest))
rendered = await svg.gemojis(rendered, {rest})
if (octicons)
rendered = await svg.octicons(rendered)
rendered = marked.parse(rendered)
//Render through browser and print pdf
console.debug("metrics/svg/pdf > loading svg")
Expand Down Expand Up @@ -550,6 +552,26 @@ export const svg = {
rendered = rendered.replace(new RegExp(emoji, "g"), gemoji)
return rendered
},
/**Render github octicons */
async octicons(rendered) {
//Load octicons
console.debug("metrics/svg/octicons > rendering octicons")
const icons = new Map()
for (const {name, heights, toSVG} of Object.values(octicons)) {
for (const size of Object.keys(heights)) {
const octicon = `:octicon-${name}-${size}:`
if (new RegExp(`:octicon-${name}(?:-[0-9]+)?:`, "g").test(rendered)) {
icons.set(octicon, toSVG({height:size, width:size}))
if (Number(size) === 16)
icons.set(`:octicon-${name}:`, toSVG({height:size, width:size}))
}
}
}
//Apply replacements
for (const [octicon, image] of icons)
rendered = rendered.replace(new RegExp(octicon, "g"), image)
return rendered
},
/**Optimizers */
optimize:{
/**CSS optimizer */
Expand Down
16 changes: 16 additions & 0 deletions source/plugins/core/metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,22 @@ inputs:
default: yes
global: yes

config_octicon:
description: |
Use GitHub octicons

Octicons are open-sourced icons provided by GitHub.
See full list at https://primer.style/octicons.

To include an octicon, use the following syntax: `:octicon-{name}-{size}:`.
Size must be a supported icon size (12, 16 or 24).
16px octicons can omit size and directly use `:octicon-{name}:` syntax.

May increase filesize
type: boolean
default: no
global: yes

config_display:
description: |
Display width (for image output formats)
Expand Down