显示雨果网站中Github Gist的文本值

发布于 2025-02-01 08:47:15 字数 1375 浏览 2 评论 0 原文

我知道我可能会问一些非常简单的事情,但是对于我的一生来说,我似乎无法摆脱困境,我肯定会监督一些简单的东西,但我不知道。任何帮助将不胜感激。

我正在使用Hugo生成一个静态网站。在我的一个页面上,我想使用一个变量来创建类似进度栏的内容,我需要从github gist中获得的变量。

说这是要点:

该文件只有一个数字,就是这样。我要问的是如何从要点获取该数字并将其存储在雨果中,或者至少将其显示在某些RAW HTML中。我想提一下,我不想使用提供的嵌入式文本,而是宁愿获得原始值。归根结底,我所需要的是在此处阅读并显示原始链接的数字: https://gist.githubusercontent.com/bogdanbacila/c5a9683089c74d613ad13ad13ad13ad13ad18f56b/raw/raw/raw/8380782afeede822afeede80d234209293333334.4949.494.4949.4494.44 b.4 b.4 b.4bnnyth y4 b.4 b.4 b.44 b.4 b.4 b.4 b.4 b.4 b.by

bund雨果论坛上的这个问题不是非常有帮助,而不是为我提供我发送到这里的一些指导。这是我最初的问题: https://discourse.gohugo.io/t/get-raw-content-from-gitrom-github-gist-gist-to-a-variable/38781

任何帮助都将不胜感激,我知道我知道有什么很明显的我“我看不到,请引导我走向正确的方向,这并不是那么复杂。

最好的, 博格丹

I know I might be asking something quite simple but for the life of me I can't seem to get my head around this and I'm definitely overseeing something simple but I don't know what. Any help would be very appreciated.

I'm generating a static site using Hugo. On one of my pages, I want to create something like a progress bar, using a variable which I need to get from a file from a Github Gist.

Say this is the gist: https://gist.github.com/bogdanbacila/c5a9683089c74d613ad17cdedc08f56b#file-thesis-words-txt

The file only has one number, that's it. What I'm asking is how to get that number from the gist and store it in hugo or at least just display it in some raw html. I want to mention that I'm not looking to use the provided embedded text, I'd rather just get the raw value. At the end of the day all I need is to read and display the number from the raw link here: https://gist.githubusercontent.com/bogdanbacila/c5a9683089c74d613ad17cdedc08f56b/raw/8380782afede80d234209293d4c5033a890e44b6/thesis-words.txt

I've asked this question on the Hugo forum and that wasn't very helpful, instead of providing me with some guidance I got sent here. Here was my original question: https://discourse.gohugo.io/t/get-raw-content-from-github-gist-to-a-variable/38781

Any help would be greatly appreciated, I know there's something very obvious which I'm not seeing, please guide me to the right direction, this doesn't feel like it should be that complicated.

Best,
Bogdan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我也只是我 2025-02-08 08:47:16

@wjh18

欢呼!我不知道得到请求,所以我不得不仔细研究一下,但是我设法将其进行了。

<script>
            fetch('https://gist.githubusercontent.com/bogdanbacila/c5a9683089c74d613ad17cdedc08f56b/raw').then(function(response) {
              return response.json();
            }).then(function(data) {
              console.log(data);
            }).catch(function() {
              console.log("Booo");
            });
</script>

@wjh18

Cheers! I didn't know about GET requests so I had to dig around for that a little bit but I managed to get it going with this:

<script>
            fetch('https://gist.githubusercontent.com/bogdanbacila/c5a9683089c74d613ad17cdedc08f56b/raw').then(function(response) {
              return response.json();
            }).then(function(data) {
              console.log(data);
            }).catch(function() {
              console.log("Booo");
            });
</script>
天涯沦落人 2025-02-08 08:47:15

您可以获取此数据并将其存储在Hugo中作为数据文件,但我不建议它。

由于Hugo是静态站点生成器,因此您不仅需要在价值变化时修改存储库中的数据文件,而且还需要重新构建网站。然后,您必须担心按时间表运行脚本。这意味着您无法确定该值是当前的,第二个人访问您的网站。这比我认为的头痛更重要。

更好的路线是编写一些客户端的JavaScript,该JavaScript致电要查索的原始URL以获取内容。这是Hugo-Agnostic,这就是为什么我怀疑您在这里指向。

来自 gists api docs:

如果您需要文件的完整内容,则可以向 raw_url

指定的URL提出获取请求

您可以使用 fetch api 或任何其他JS客户端。只需向URL提出请求,从响应主体中解析值,然后编写一些JavaScript以在某人向其打开的页面请求时插入DOM中的值。

You could fetch this data and store it in Hugo as a data file but I don't recommend it.

Since Hugo is a static site generator, you would need to not only modify the data files in your repo every time the value changes, but re-build your site as well. Then you have to worry about running the script on a schedule. Meaning you can't be sure that the value is current the second someone visits your site. This is more headache than it's worth in my opinion.

The better route would be to write some client-side JavaScript that makes a call to the raw URL of the gist to get the content. This is Hugo-agnostic which is why I suspect you were pointed here.

From the Gists API docs:

If you need the full contents of the file, you can make a GET request to the URL specified by raw_url.

You can use something like the Fetch API for this or any other JS client. Simply make a GET request to the URL, parse the value from the response body, and write some JavaScript to insert the value in the DOM when someone makes a request to the page it's on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文