【问题标题】:Create a Github badge创建 Github 徽章
【发布时间】:2021-11-08 20:11:47
【问题描述】:

我想为 Github README 创建一个徽章(例如 this)。我创建了一个函数来使用 JavaScript 从 URL 获取 GET 值并将其放入 SVG,当我在本地尝试它时可以使用,但如果我将它放入 Markdown 文件中,它不会更改数字。

![No](http://luigitest.altervista.org/widget/font.svg?years=9)

index.svg:

<svg width="200" height="75" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
style="-webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;">
  

<g>  
  <rect x="0" y="0" width="125" height="75" style="fill:#F84949F5"></rect>
  <text x="0" y="37.5" font-family="Segoe UI" font-size="16"  font-weight="bold" fill="white">
    <tspan x="62.5" text-anchor="middle" dy="-3.5%">I started</tspan>
    <tspan x="62.5" text-anchor="middle" dy="25%">programming</tspan>
  </text>
  <rect x="124" y="0" width="75" height="75" style="fill:#F84949"></rect>
  <text x="124" y="75" font-family="Segoe UI" font-size="13" fill="white" font-weight="bold">
    <tspan x="162.5" text-anchor="middle" y="45.891675" font-size="40" id="years-number">5</tspan>
    <tspan x="162.5" text-anchor="middle" y="67.5"  font-weight="bold">years ago</tspan>
  </text>
</g>  
<script xlink:href="script.js" />
</svg>

script.js:

function changeNumber() {
    document.getElementById("years-number").textContent = getValue;
}

let getValue;

window.addEventListener("load", function () {
    let name="years";
    if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
      getValue = decodeURIComponent(name[1]);
  
    changeNumber();
});

在浏览器中的输出: correct output

markdown 中的输出(正确的一个): wrong output

我该如何解决?

【问题讨论】:

  • github 是 https,我不认为你可以通过 http:// 加载远程资源,你得到混合内容的问题,你可能需要添加 ssl
  • 尝试将 svg 放入 svg 元素中,如下所示:stackoverflow.com/questions/5378559/including-javascript-in-svg
  • 我尝试使用带有 SSL 的网站,但没有任何变化@LawrenceCherone
  • 甚至没有将 js 放在 svg 元素中它可以工作@enxaneta
  • script.js 在 index.svg 中,所以它不会被执行。其他图标必须以不同的方式工作。也许他们使用 SMIL 或 CSS 动画。

标签: javascript github svg markdown


【解决方案1】:

您可以使用模板 svg 和 pupa 创建无服务器函数。

Svg 文件:

...svg
    <tspan x="162.5" text-anchor="middle" y="45.891675" font-size="40" id="years-number">{years}</tspan>
...svg

服务器端代码:

// https://stackoverflow.com/questions/69164294/create-a-github-badge
import pupa from 'pupa'
import { readFileSync } from 'fs'
import { join } from 'path'
import { dirname } from 'dirname-filename-esm'

const __dirname = dirname(import.meta)
const template = readFileSync(join(__dirname, '../badge.svg'), 'utf8')

export default (req, res) => {
  let { years } = req.query
  if (typeof years !== 'string') return res.status(400).end()
  res.setHeader('Content-Type', 'text/xml')
  res.end(pupa(template, { years }))
}

我使用Vercel 来部署无服务器功能。除了 Vercel,还有其他方法。

回复链接:https://replit.com/@Programmerraj/serverless-api#api/index.js

链接到实时示例:https://serverless-api-ebon.vercel.app/api?years=6(将 years 更改为任何内容)。

GitHub 存储库链接:https://github.com/ChocolateLoverRaj/serverless-api

在这个答案中用 Markdown 编写的预览:

【讨论】:

  • 我想添加第二个参数(颜色)但做let { years } = req.query.years; let color = req.query.color(而不是const template = readFileSync(join(__dirname, '../'+color+'.svg'), 'utf8');给我带来The character encoding of the plain text document was not declared之类的问题。我还尝试了其他方法来获取参数(@ 987654325@) 并使用 if 以不同方式声明变量“模板”,但它仍然不起作用。有关如何解决此问题的任何想法?
  • 为什么每种颜色都需要一个单独的文件?为什么不把颜色作为模板的一部分。
  • 这是个好主意,但错误仍然存​​在
猜你喜欢
  • 2017-12-17
  • 2017-01-25
  • 2018-08-14
  • 2016-04-01
  • 2021-09-17
  • 2020-01-03
  • 2021-04-25
  • 2021-02-13
  • 1970-01-01
相关资源
最近更新 更多