如何按标题对降价部分进行排序?
How to sort sections of markdown by heading?
给定一个结构如下的大型降价文件:
# Main
## Sub main Z
### Title Z
Content Z
### Title B
Content B
## Sub main A
### Title A
Content A
如何对其部分进行排序,以便它们按标题的字母顺序排列?
# Main
## Sub main A
### Title A
Content A
## Sub main Z
### Title B
Content B
### Title Z
Content Z
我认为没有任何解决方案不需要您编写一些代码。
但如果您会编码,编程解决方案将非常简单:
- 将 Markdown 解析为 AST(抽象语法树)。
- 操纵 AST,根据需要重新排序节点。
- 将修改后的 AST 写回 Markdown。
诀窍是选择一个支持上述步骤的 Markdown 解析器,它可以让您以您选择的语言进行操作,并且可以让您最轻松地进行操作。
以下是我个人认识的符合这些要求的:
Pandoc
可能是世界上排名第一的 Markdown 工具包。 Pandoc 的母语是 Haskell,但它支持多种语言。如果你打算在未来做很多 Markdown 的事情,那么无论如何在 Pandoc 中变得知识渊博可能是有意义的。
它有一个选项,--section-divs
,将文档转换为基于标题的部分层次结构,这将使每个嵌套级别的重新排序部分变得几乎微不足道(如果你会编码) .
它有(支持过滤器](https://pandoc.org/filters.html#),这正是我描述的解决方案。
它特别支持 Lua 和 Lua filters,这可能是最容易编码的。
你也可以write filters in other languages: Python, PHP, Perl, Javascript/Typescript, Groovy, Ruby .
A quick web search 显示了许多示例过滤器。通过更多的搜索,您可能会幸运地找到一个已经完成您想要的内容,尽管我对此表示怀疑,因为按字母顺序对部分进行排序似乎是一件不常见的事情。
CMark
CommonMark 的 C 参考实现。支持 C 以外的语言:
It provides a shared library (libcmark) with functions for parsing CommonMark documents to an abstract syntax tree (AST), manipulating the AST, and rendering the document to HTML, groff man, LaTeX, CommonMark, or an XML representation of the AST. It also provides a command-line program (cmark) for parsing and rendering CommonMark documents.
- Flexible. CommonMark input is parsed to an AST which can be manipulated programmatically prior to rendering.
- Multiple renderers. Output in HTML, groff man, LaTeX, CommonMark, and a custom XML format is supported. And it is easy to write new renderers to support other formats.
It is easy to use libcmark in python, lua, ruby, and other dynamic languages: see the wrappers/ subdirectory for some simple examples.
remark
一个Javascript-based 专门围绕 AST 操作设计的框架。我从未使用过它,但它可能具有使 AST 操作更容易的工具,尽管我只是猜测。
祝你好运!
我尝试使用 pandoc
,但是在从 AST 到 markdown 的步骤中丢失了许多格式,如 new line
等
我找到了另一种方法。我们只需要 MS Word
.
- 打开markdown文件以为
Word
- 使用下一个代码创建新的宏。我们找到所有 headers (
#
) 并应用样式 Title 1
、Title 2
等等。给你换个样式的名字。
Sub insertStyleHeaders()
Dim hashCount As Integer
Dim styleStr As String
Set oRng = ActiveDocument.Range
With oRng.Find
.text = "#"
While .Execute
oRng.MoveEnd wdParagraph
If InStr(oRng.text, "# ") Then
hashCount = Len(oRng.text) - Len(Replace(oRng.text, "#", ""))
styleStr = "Title " + CStr(hashCount)
oRng.Select
oRng.Style = ActiveDocument.Styles(styleStr)
End If
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
- 在运行宏之后,鼠标右键点击:
Expand/Collapse
-> Collapse All Headings
- 现在我们有了下一个文档。您需要 select 按鼠标分组并按标题调用排序。
- 排序结束后,select 所有文档 (
Ctrl + A
) 并应用 plain text
/default
样式以删除 header 样式。
- 最后,使用标尺移动缩进并保存文件。
给定一个结构如下的大型降价文件:
# Main
## Sub main Z
### Title Z
Content Z
### Title B
Content B
## Sub main A
### Title A
Content A
如何对其部分进行排序,以便它们按标题的字母顺序排列?
# Main
## Sub main A
### Title A
Content A
## Sub main Z
### Title B
Content B
### Title Z
Content Z
我认为没有任何解决方案不需要您编写一些代码。
但如果您会编码,编程解决方案将非常简单:
- 将 Markdown 解析为 AST(抽象语法树)。
- 操纵 AST,根据需要重新排序节点。
- 将修改后的 AST 写回 Markdown。
诀窍是选择一个支持上述步骤的 Markdown 解析器,它可以让您以您选择的语言进行操作,并且可以让您最轻松地进行操作。
以下是我个人认识的符合这些要求的:
Pandoc
可能是世界上排名第一的 Markdown 工具包。 Pandoc 的母语是 Haskell,但它支持多种语言。如果你打算在未来做很多 Markdown 的事情,那么无论如何在 Pandoc 中变得知识渊博可能是有意义的。
它有一个选项,
--section-divs
,将文档转换为基于标题的部分层次结构,这将使每个嵌套级别的重新排序部分变得几乎微不足道(如果你会编码) .它有(支持过滤器](https://pandoc.org/filters.html#),这正是我描述的解决方案。
它特别支持 Lua 和 Lua filters,这可能是最容易编码的。
你也可以write filters in other languages: Python, PHP, Perl, Javascript/Typescript, Groovy, Ruby .
A quick web search 显示了许多示例过滤器。通过更多的搜索,您可能会幸运地找到一个已经完成您想要的内容,尽管我对此表示怀疑,因为按字母顺序对部分进行排序似乎是一件不常见的事情。
CMark
CommonMark 的 C 参考实现。支持 C 以外的语言:
It provides a shared library (libcmark) with functions for parsing CommonMark documents to an abstract syntax tree (AST), manipulating the AST, and rendering the document to HTML, groff man, LaTeX, CommonMark, or an XML representation of the AST. It also provides a command-line program (cmark) for parsing and rendering CommonMark documents.
- Flexible. CommonMark input is parsed to an AST which can be manipulated programmatically prior to rendering.
- Multiple renderers. Output in HTML, groff man, LaTeX, CommonMark, and a custom XML format is supported. And it is easy to write new renderers to support other formats.
It is easy to use libcmark in python, lua, ruby, and other dynamic languages: see the wrappers/ subdirectory for some simple examples.
remark
一个Javascript-based 专门围绕 AST 操作设计的框架。我从未使用过它,但它可能具有使 AST 操作更容易的工具,尽管我只是猜测。
祝你好运!
我尝试使用 pandoc
,但是在从 AST 到 markdown 的步骤中丢失了许多格式,如 new line
等
我找到了另一种方法。我们只需要 MS Word
.
- 打开markdown文件以为
Word
- 使用下一个代码创建新的宏。我们找到所有 headers (
#
) 并应用样式Title 1
、Title 2
等等。给你换个样式的名字。
Sub insertStyleHeaders()
Dim hashCount As Integer
Dim styleStr As String
Set oRng = ActiveDocument.Range
With oRng.Find
.text = "#"
While .Execute
oRng.MoveEnd wdParagraph
If InStr(oRng.text, "# ") Then
hashCount = Len(oRng.text) - Len(Replace(oRng.text, "#", ""))
styleStr = "Title " + CStr(hashCount)
oRng.Select
oRng.Style = ActiveDocument.Styles(styleStr)
End If
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
- 在运行宏之后,鼠标右键点击:
Expand/Collapse
->Collapse All Headings
- 现在我们有了下一个文档。您需要 select 按鼠标分组并按标题调用排序。
- 排序结束后,select 所有文档 (
Ctrl + A
) 并应用plain text
/default
样式以删除 header 样式。 - 最后,使用标尺移动缩进并保存文件。