GitHub Markdown: 是否支持宏和变量?

79

我一直在学习Github标记语言,我对变量和宏有一个问题。

是否可以定义一个变量或宏来防止重复打印一块文本?

使用场景是我有一个表格,产生一个大的超链接网格 - 链接看起来像下面这样。

http://www.a-big-long-big-big-long-hyperlink/more-long-stuff?id=1234

如果我能像下面这样做就好了:

$link=http://www.a-big-long-big-big-long-hyperlink/more-long-stuff?id

然后在表格的每个单元格中,我可以这样说

$link=1234

其他一些单元格

$link=2345

这个想法是:

  • 在普通屏幕上,这个表格(大约有10列和10行)更容易查看。目前链接的前缀太长了,导致链接换到下一行时非常难看。
  • 如果我想要改变根链接,我只需在一个地方进行更改(是的,我知道可以在编辑器中进行搜索和替换!)

谢谢。

5个回答

17

以下是几种编写参考链接的方式

[I'm an inline-style link](https://www.somewebsite.com)

[I'm an inline-style link with title](https://www.somewebsite.com "somewebsite's Homepage")

[I'm a reference-style link][Arbitrary case-insensitive reference text]

[I'm a relative reference to a repository file](../blob/master/LICENSE)

[You can use numbers for reference-style link definitions][1]

Or leave it empty and use the [link text itself]

Some text to show that the reference links can follow later.

[arbitrary case-insensitive reference text]: https://www.somewebsite.org
[1]: http://somewebsite.org
[link text itself]: http://www.somewebsite.com

74
我不确定这个回答是否符合问题。Op 是否希望定义一个变量,例如 url 的根路径,并在许多地方使用它,而仅提供更改部分(ID)? - Gerry
2
文本的上下文是否可以转换为链接?例如,[FOO-11](JIRA) 转换为 https://jira.example.net/browse/FOO-11 - Mikhail T.
1
@Gerry,它确实帮了我。我试图避免重新输入http://github.com/myrepo/...,@Adi的建议在这里非常有帮助...谢谢 :) - Aakash

6

19
这很好,但没有回答问题。 - robi-y

1
这在基本的Markdown中是不可能的,但是有一些扩展可以添加宏和变量。(示例。)

-1
如果你只使用Markdown语法而不是HTML,你可以使用括号来实现这一点。
首先,你需要创建变量:
[foo]: https://example1.com
[bar]: https://example2.com

然后使用方括号中的名称访问变量。
I am the first [variable][foo]

它甚至在StackOverflow上也能运行。

我是第二个变量


这并没有回答问题。 - undefined

-6

GitHub Markdown(用于 .md 文件)通过 capture 实现变量:

{% capture nameOfVariableToCapture %}any markdown here...{% endcapture %}

{{ nameOfVariableToCapture }} -- that prints the content of the variable

或者从{% assign variableName = "文本等等" %}中获取。

作为一个测试,我创建了https://github.com/SeLite/SeLite.github.io/blob/master/MarkdownTest.md。您可以在http://selite.github.io/MarkdownTest上查看其内容(忽略标题和页脚,这来自一个框架)。


4
看起来好像并不行。这的确是一个神秘的回答。“你可能会从中受益”——只要你能让它起作用 :). - kkm
5
GitHub Pages 使用 Jekyll 来解析 模板语言 Liquid。 就是这样。更多关于 Jekyll gh-pages 的信息:https://jekyllrb.com/docs/github-pages/我还找到了一个 stackoverflow 问题,其中包含有关 GitHub 上变量的示例:https://stackoverflow.com/questions/41606739/what-the-the-scope-of-liquid-variables-on-jekyll-on-github-pages - olavimmanuel
4
对于 Github Pages 来说这很有用(如果您的项目在 Github 上,那就不需要额外添加了),但值得澄清的是,它不能用于在 Github 自己上内联显示 markdown 文件,例如 README.md 文件。这是因为 Markdown 是一种格式而不是可执行语言。 - Adam Burke
看起来原问题特别询问了关于Github Markdown的问题,所以这似乎是合法的。也许我错过了什么... - Robb Sadler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接