【问题标题】:How to embed only certain lines from a gist?如何仅嵌入要点中的某些行?
【发布时间】:2019-09-11 04:57:34
【问题描述】:

Gist 可让您在网页中嵌入要点:

<script src="https://gist.github.com/dyoo/4627246.js"></script>

我希望我可以让它只显示一个行范围子集。 (用例:在一个要点中为博客文章编写一些代码。在整个博客文章中引用某些行来讨论这些行。)

这可能吗?我找不到它的文档,但是有什么方法可以做到吗?


作为我认为它可能如何工作的一个例子,GitHub 支持带有行号的 URI。例如,#L4-L8 中的锚点:

https://github.com/twitter/bootstrap/blob/master/docs/base-css.html#L4-L8

我意识到 GitHub 仅显示 所有 行,而这些行突出显示。而我问的是如何显示 Gist 中的某些行。我只是举个例子,我猜它可能会起作用。


p.s 根据How do I embed a single file from a GitHub gist with the new gist interface? 的说法,这听起来最近与我所问的方向相反:从要点中嵌入一个文件的能力最近消失了。我只想显示一个文件中的某些行。但我还是想问。另外我认为这是一个与那个不同的问题,这就是为什么我发布这个而不是评论现有问题。

【问题讨论】:

  • 实际上使用?file=参数的旧方法仍然有效,但似乎不再在UI中实现。

标签: github gist


【解决方案1】:

Gist API 不提供任何这样的方式来嵌入部分要点。但是,您最终可以使用 javascript 自己完成。 你可以使用一个很好的库:

https://github.com/kashif-umair/gist-embed/

您可以在其中嵌入部分要点。

【讨论】:

【解决方案2】:

你需要的是这个:

http://gist-it.appspot.com/

# Embed the file robertkrimen/gist-it-example/example.js
<script src="http://gist-it.appspot.com/github/robertkrimen/gist-it-example/blob/master/example.js"></script>

# Embed without a footer
<script src="http://gist-it.appspot.com/github/robertkrimen/gist-it-example/blob/master/example.js?footer=0"></script>

# Show only the first and second line
<script src="http://gist-it.appspot.com/github/robertkrimen/gist-it-example/blob/master/example.js?slice=0:1"></script>

【讨论】:

    【解决方案3】:

    有一种方法可以在没有库的情况下做到这一点

    复制嵌入并将以下?file=the-name-of-your-file附加到src属性


    一个真实的例子

    要点是https://gist.github.com/dianjuar/8df3873cd9db49b7fcd86e74b57dd848

    所以,要在嵌入中仅显示名为 circleci-useful-env-variables-pr.sh 的文件,我必须

    <script src="https://gist.github.com/dianjuar/8df3873cd9db49b7fcd86e74b57dd848.js?file=circleci-useful-env-variables-pr.sh"></script>
    

    【讨论】:

      猜你喜欢
      • 2020-10-29
      • 2021-02-11
      • 2011-06-03
      • 1970-01-01
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      相关资源
      最近更新 更多