代码之家  ›  专栏  ›  技术社区  ›  recipriversexclusion

如何在标记中链接到同一文档的一部分?

  •  361
  • recipriversexclusion  · 技术社区  · 16 年前

    我正在编写一个大的降价文档,并希望在开头放置一个分类目录,它将提供指向文档中各个位置的链接。我该怎么做?

    我试着用

    [a link](# MyTitle)
    

    哪里 MyTitle 是文档中的标题,但这不起作用。

    11 回复  |  直到 8 年前
        1
  •  35
  •   leemes    14 年前

    pandoc ,如果使用选项 --toc 在生成HTML时,将生成一个目录,其中包含指向节的链接,以及从节标题返回到目录的链接。它类似于其他格式的PanDoc写入,如LaTex、RTF、RST等,因此使用命令

    pandoc --toc happiness.txt -o happiness.html
    

    这一点降价:

    % True Happiness
    
    Introduction
    ------------
    
    Many have posed the question of true happiness.  In this blog post we propose to
    solve it.
    
    First Attempts
    --------------
    
    The earliest attempts at attaining true happiness of course aimed at pleasure. 
    Soon, though, the downside of pleasure was revealed.
    

    将生成HTML的正文:

        <h1 class="title">
            True Happiness
        </h1>
        <div id="TOC">
            <ul>
                <li>
                    <a href="#introduction">Introduction</a>
                </li>
                <li>
                    <a href="#first-attempts">First Attempts</a>
                </li>
            </ul>
        </div>
        <div id="introduction">
            <h2>
                <a href="#TOC">Introduction</a>
            </h2>
            <p>
                Many have posed the question of true happiness. In this blog post we propose to solve it.
            </p>
        </div>
        <div id="first-attempts">
            <h2>
                <a href="#TOC">First Attempts</a>
            </h2>
            <p>
                The earliest attempts at attaining true happiness of course aimed at pleasure. Soon, though, the downside of pleasure was revealed.
            </p>
        </div>
    
        2
  •  579
  •   Jeff Puckett    9 年前

    GitHub会自动从您的头中解析定位标记。因此,您可以执行以下操作:

    [Foo](#foo)
    
    # Foo
    

    在上述情况下, Foo 头已生成名为的定位标记 foo

    注释 只有一个 # 对于所有标题大小,之间没有空格 γ 和锚定名称,锚定标记名称必须是小写,如果是多个单词,则用破折号分隔。

    [click on this link](#my-multi-word-header)
    
    ### My Multi Word Header
    

    更新

    开箱即用 pandoc 也是。

        3
  •  90
  •   Steve Powell    9 年前

    在实验中,我发现了一个解决方案 <div…/> 但是一个明显的解决方案是在页面中任意位置放置您自己的定位点,因此:

    <a name="abcde">
    

    之前

    </a>
    

    之后 要“链接”到的行。然后是一个降价链接,比如:

    [link text](#abcde)
    

    文件中的任何地方都会带你去那里。

    这个 <div / /gt; 解决方案插入一个“虚拟”除法,只需添加 id 属性,这可能会破坏页面结构,但是 <a name="abcde"/> 解决办法应该是相当无害的。

    (附言:抛锚也许没问题。 在里面 要链接到的行,如下所示:

    ## <a name="head1">Heading One</a>
    

    但这取决于降价是如何处理的。我注意到,例如,堆栈溢出应答格式化程序对此很满意!)

        4
  •  65
  •   Ally    12 年前

    这可能是过时的线程,但要在Github中创建内部文档链接,请使用…
    (注意:小写标题)

        # Contents
         - [Specification](#specification) 
         - [Dependencies Title](#dependencies-title) 
    
        ## Specification
        Example text blah. Example text blah. Example text blah. Example text blah. 
    Example text blah. Example text blah. Example text blah. Example text blah. 
    Example text blah. Example text blah. Example text blah. Example text blah. 
    Example text blah. Example text blah. 
    
        ## Dependencies Title
        Example text blah. Example text blah. Example text blah. Example text blah. 
    Example text blah. Example text blah. Example text blah. Example text blah. 
    Example text blah. Example text blah. Example text blah. Example text blah. 
    Example text blah. Example text blah. 
    

    我提出了一个很好的问题,所以我修改了我的答案;

    内部链接可以使用- # , ## , ### , #### 我在下面创建了一个快速示例… https://github.com/aogilvie/markdownLinkTest

        5
  •  17
  •   davidj411    11 年前

    是的,Markdown会这样做,但您需要指定名称锚 <a name='xyx'> .

    一个完整的例子,

    这将创建链接
    [tasks](#tasks)

    在文档的后面,您将创建命名锚(无论它叫什么)。

    <a name="tasks">
       my tasks
    </a>
    

    请注意,您也可以将它环绕在头上。

    <a name="tasks">
    ### Agile tasks (created by developer)
    </a>
    
        6
  •  9
  •   hoijui    9 年前

    PanDoc手册解释了如何使用标题的标识符链接到标题。我没有检查其他解析器对此的支持,但据报告 在GitHub上不工作 .

    可以手动指定标识符:

    ## my heading text {#mht}
    Some normal text here,
    including a [link to the header](#mht).
    

    或者可以使用自动生成的标识符。两者都在 pandoc manual .

    注释 这个 只有 转换为时工作 HTML , 乳胶 , 语境 , 织物 阿西多克 .

        7
  •  7
  •   Nick Gerakines    16 年前

    降价规范中没有这样的指示。对不起。

        8
  •  2
  •   Adam    8 年前

    GITLAB使用 GitLab Flavored Markdown (GFM)

    这里“所有降价渲染的标题自动获取ID”

    可以使用鼠标:

    • 将鼠标移到标题上
    • 将鼠标移到从标题向左可见的悬停选择器上
    • 使用鼠标右键单击复制并保存链接

      例如,在readme.md文件中,我有头文件:

    ## series expansion formula of the Boettcher function

    它提供了一个链接:

    https://gitlab.com/adammajewski/parameter_external_angle/blob/master/README.md#series-expansion-formula-of-the-boettcher-function

    可以删除前缀,因此此处的链接

    file#header
    

    这意味着:

    README.md#series-expansion-formula-of-the-boettcher-function
    

    现在它可以用作:

    [series expansion formula of the Boettcher function](README.md#series-expansion-formula-of-the-boettcher-function)

    也可以手动操作:用连字符符号替换空格。

    活生生的例子 here

        9
  •  1
  •   Dmitry Shvedov nozzleman    10 年前

    使用kramdown,似乎效果很好:

    [I want this to link to foo](#foo)
    ....
    ....
    {: id="foo"}
    ### Foo are you?
    

    我看到有人提到

    [foo][#foo]
    ....
    #Foo
    

    有效地工作,但是前者可能是除头之外的元素或具有多个单词的头之外的元素的一个很好的选择。

        10
  •  1
  •   jwpfox Amit    9 年前

    因为在评论中提到了多重降价。

    MultiMarkdown 内部链接的语法很简单。

    对于文档中的任何标题,只需给出此格式的标题名称 [heading][] 创建内部链接。

    在这里阅读更多: MultiMarkdown-5 Cross-references .

    交叉引用

    一个经常被要求的特性是能够让降价自动处理文档链接内,就像它处理外部链接一样容易。为此,我添加了将[某些文本]]解释为交叉链接的能力,前提是存在名为_某些文本_的头。

    作为一个例子,[元数据]]将带您进入元数据(或任何元数据)。

    或者,您可以包含一个可选标签,用于帮助消除多个标题具有相同标题的情况:

    ###概述[MultimarkdownOverview]##

    这允许您使用[multimarkdownoverview]专门引用此部分,而不是另一个名为overview的部分。这适用于ATX或SETTExt样式的头文件。

    如果已经使用头使用的相同ID定义了锚,则定义的锚将优先。

    除了文档中的标题之外,还可以为图像和表提供标签,然后这些标签也可用于交叉引用。

        11
  •  0
  •   laggingreflex    8 年前

    还有一些旋转 <a name=""> 诀窍:

    <a id="a-link"></a> Title
    ------
    

    #### <a id="a-link"></a> Title (when you wanna control the h{N} with #'s)