84

有没有办法在 Markdown 文档中指明文档标题?

我开始使用 Markdown 和 Sublime Text 来准备我的许多个人和商业文件。例如,我经常希望有一种类似于 Word 中标题样式的“顶级”标题。因此,例如:

### Things to Do ###

At Home
=======
*    Mow the cat
*    Feed the lawn

At the Office
=============
*    Learn Markdown
*    Use Big-O notation in a clever way

但是### Things to Do ###Markdown不尊重这条线,我不知道有什么替代方案。有吗?

我可以对标题使用标题 1 样式,然后对其余部分使用标题 2,但如果我需要更深层次的标题嵌套,我很快就会用尽深度。毕竟,标题本质上不是标题本身。<title>例如,如果 Markdown-to-HTML 解析器使用页面的标题以及页面顶部的标题(例如 Word 标题),那就太好了。

4

6 回答 6

52

如果您具体指的是 pandoc markdown,最简单的方法是使用 '%',例如

% Document Title

# Header 1

content

## Header 2

## Header 2

有关 pandoc markdown 的更多信息,请参阅http://pandoc.org/README.html#metadata-blocks

于 2015-06-11T00:16:20.727 回答
39

我用 Markdown 写书和研究论文,专门在 GitHub 上发布,而 Markdown 中的 HTML 标题标签在 GitHub 上不起作用,所以我使用以下约定:

Document Title
==============

***This is a subtitle***

**Author:** *Me*

# Chapter One: Overview

Do you know the way?

---

# Chapter Two: Foo

Foo is the way...

---

最终看起来像:


文件名

这是字幕

作者:

第一章:概述

你知道路吗?


第二章 Foo

福是路...


我使用---来分隔章节,因为它看起来不错并且有助于在文本中找到章节。但是,当 Markdown 文档变大时,这确实会出现问题,在这种情况下,Markdown 预览窗口会在您每次键入时开始冻结,因为它会刷新或 Grammarly 开始出错并需要很长时间。这是使用 H1 标题格式的理由,===因为当文档变大时,您需要将其分解,在这种情况下,最好使用以下格式:

Document Title
==============

***This is a subtitle***

**Author:** *Me*

[<< Previous Chapter](URL) | [Content Table](URL) | [Next Chapter >>](URL)

---

# Chapter Two: Foo

Foo is the way...

---

[<< Previous Chapter](URL) | [Content Table](URL) | [Next Chapter >> ](URL)

然后看起来像:


文件名

这是字幕

作者:

<< 上一章| 目录| 下一章>>


第二章 Foo

福是路...


<< 上一章| 目录| 下一章>>


我也放弃了使用 Wiki 文件名作为标题,因为它不允许连字符,这会弄乱我的章节标题,所以我已经切换到所有以章节索引01_chapter_name.md, 02_chapter_name-with-hyphens.md, ...开头的小写文件名===H1 标题格式并将我的 Markdown 书籍移动到主存储库,这样我就可以使用问题驱动开发和 GitHub 问题和项目,每章一个项目,这样我就可以记住所有要做的事情并完成积压工作。

于 2018-09-08T16:32:32.720 回答
26

Markdown 设计的有趣之处之一是明确允许使用 HTML。HTML5 添加了语义页面部分,包括 <header> 和 <main>,这可能非常适合您的页面标题。

例如:

<header>
Things to Do
============
</header>
<main>
At Home
=======
*    Mow the cat
*    Feed the lawn

At the Office
=============
*    Learn Markdown
*    Use Big-O notation in a clever way
</main>

如果您更喜欢排除 HTML,您可能需要使用 Atx 样式的标题以获得两个以上的层次结构。

例如:

# Things to Do

## At Home
*    Mow the cat
*    Feed the lawn

## At the Office
### Morning
*    Learn Markdown
*    Use Big-O notation in a clever way
### Afternoon
*    Read e-mails
*    Scrutinize LOLcats
于 2013-06-13T22:17:10.850 回答
7

标题元数据

如果您使用 MultiMarkdown,您可以在文档顶部添加一些元数据

format: complete
title: This is a title for the web-page
css: http://example.com/main.css

First line of visible text

标题将包含在<title>部分<head>

您也可以使用[%title])通过引用将其包含在正文中

小标题

###在第一行的开头识别为生成<h3>标签的 3 级标题应该没有任何问题。我在 Markdown/MultiMarkdown 的几个实现中使用它

您可以使用John Gruber 的 DingusMarkable等对其进行测试。

航向偏移

至少一些 Markdown/Multimarkdown 实现允许您为生成的标题指定偏移量,以便它生成<h2>and<h3>而不是<h1>and <h2>

这将允许您放置,例如,<h1>Title</h1><h1>[%title]</h1>作为文档的第一行(在元数据声明之后)。

参考

于 2015-02-05T17:27:52.343 回答
3

我想我会发布一个蛮力一次性解决方案:

<font size="+12"><center>
    Things to Do
</center></font>

肯定有一种更复杂的方法可以做到这一点,但我发现由于每个文档都使用一次,所以还不错。

于 2020-02-12T02:12:35.177 回答
0

如果您不介意使用 RStudio,Rmd (rmarkdown) 文件会使用顶部的元数据部分生成标题,然后#+用于标题。

链接

于 2018-11-29T17:05:07.070 回答