是否有任何markdown fork允许你引用其他文件,比如包含文件?具体来说,我想创建一个单独的markdown文件,其中包含我经常调用但不总是调用的链接(调用此B.md),然后当我通过引用链接到我正在写入的md文件(A.md)时,我希望它从另一个文件(B.md)中拉出链接,而不是从当前文件(A.md)的末尾。


当前回答

简短的回答是否定的。长一点的答案是肯定的。: -)

Markdown的设计目的是让人们能够编写简单、可读的文本,这些文本可以很容易地转换为简单的HTML标记。它不做文档布局。例如,没有真正的方法来将图像向左或向右对齐。至于你的问题,在任何版本的markdown中都没有包含从一个文件到另一个文件的单个链接的markdown命令(据我所知)。

与此功能最接近的是Pandoc。Pandoc允许您将文件合并为转换的一部分,这允许您轻松地将多个文件呈现为单个输出。例如,如果你正在创建一本书,那么你可以有这样的章节:

01_preface.md
02_introduction.md
03_why_markdown_is_useful.md
04_limitations_of_markdown.md
05_conclusions.md

你可以在同一个目录下执行这个命令来合并它们:

pandoc *.md > markdown_book.html

因为pandoc会在翻译之前合并所有的文件,你可以像这样在最后一个文件中包含你的链接:

01_preface.md
02_introduction.md
03_why_markdown_is_useful.md
04_limitations_of_markdown.md
05_conclusions.md
06_links.md

这是你序言的一部分。Md可以是这样的:

I always wanted to write a book with [markdown][mkdnlink].

也是你02_introduction的一部分。Md可以是这样的:

Let's start digging into [the best text-based syntax][mkdnlink] available.

只要你的最后一个文件包含这行:

[mkdnlink]: http://daringfireball.net/projects/markdown

...之前使用的相同命令将执行合并和转换,同时始终包含该链接。只要确保在文件的开头留下一到两行空白即可。pandoc文档说,它在以这种方式合并的文件之间添加了一个空行,但如果没有空行,这对我来说行不通。

其他回答

另一个使用markdown-it和jQuery的基于html的客户端解决方案。下面是一个小的HTML包装作为主文档,它支持无限的markdown文件的include,但不支持嵌套include。在JS注释中提供了解释。错误处理略。

<script src="/markdown-it.min.js"></script>
<script src="/jquery-3.5.1.min.js"></script>

<script> 
  $(function() {
    var mdit = window.markdownit();
    mdit.options.html=true;
    // Process all div elements of class include.  Follow up with custom callback
    $('div.include').each( function() {
      var inc = $(this);
      // Use contents between div tag as the file to be included from server
      var filename = inc.html();
      // Unable to intercept load() contents.  post-process markdown rendering with callback
      inc.load(filename, function () {
        inc.html( mdit.render(this.innerHTML) );
      });
  });
})
</script>
</head>

<body>
<h1>Master Document </h1>

<h1>Section 1</h1>
<div class="include">sec_1.md</div>
<hr/>
<h1>Section 2</h1>
<div class="include">sec_2.md</div>

vcode -markdown-preview-enhanced支持@import语法

https://github.com/shd101wyy/vscode-markdown-preview-enhanced

这可能意味着它是底层工具的一部分

https://github.com/shd101wyy/mume

以及其他基于mume的工具

https://github.com/gabyx/TechnicalMarkdown

下面是我如何在我的文档中使用它的一个例子,我用vcode -markdown-preview-enhanced处理:


[[Epigenetics]]
@import "epigenetics.md"

阿西多克其实是类固醇的减价品。总的来说,Asciidoc和Markdown看起来非常相似,而且很容易切换。与markdown相比,Asciidoc的一个巨大好处是它已经支持include,用于其他Asciidoc文件,还支持任何您喜欢的格式。您甚至可以根据所包含文件中的行号或标记部分包含文件。

当你写文档时,包含其他文件真的是一个救星。

例如,你可以有一个包含以下内容的asciidoc文件:

// [source,perl]
// ----
// include::script.pl[]
// ----

并在script.pl中维护您的示例

我相信你会想,是的,Github也支持asciidoc。

我的解是用m4。大多数平台都支持它,并且包含在binutils包中。

首先在文件中包含一个宏changequote(),以将引用字符更改为您喜欢的字符(默认为“)。处理文件时将删除宏。

changequote(`{{', `}}')
include({{other_file}})

在命令行中:

m4 -I./dir_containing_other_file/ input.md > _tmp.md
pandoc -o output.html _tmp.md

如果您正在使用pandoc进行markdown处理,除了在调用pandoc时使用多个输入markdown文件外,还没有本地解决方案(在https://github.com/jgm/pandoc/issues/553中讨论)。

然而,使用codebraid(实际上是为了包括自动生成的内容Markdown)可以实现这一点:

This is the content of the main Markdown file `main.md`. 
Below this line, the content of the file `chapter01.md` is included:

```{.python .cb.run}
with open('chapter01.md') as fp:
    print(fp.read())
```

This line is printed below the external content.

要将其转换为任何输出格式,可以使用如下代码:

codebraid pandoc main.md --to markdown

虽然codebraid可能被认为是“只是”包括外部Markdown文件,但它允许更多,例如包括来自外部来源的CSV或Excel表格:

Details are shown in the following table:

```{.python .cb.run}
import pandas as pd
table = pd.read_csv('table.csv')
print(table.to_markdown())
```