简体   繁体   English

增加Markdown文件中的标题级别

[英]Increase the headline level in a Markdown file

I have the following Markdown file: 我有以下Markdown文件:

# Headline 1 #

Some text

## Subheadline 1 ##

More text

# Headline 2 #

Even more text

and I'd like to deepen the headline level into: 并且我想将标题级别深化为:

## Headline 1 ##

Some text

### Subheadline 1 ###

More text

## Headline 2 ##

Even more text

I'm talking about 300 pages of text so doing it manually would be a PITA. 我说的是300页文字,因此手动执行将是PITA。 I'm happy with a vim , sed , bash , Sublime and Atom solution to that "simple" problem. 我对解决这个“简单”问题的vimsedbashSublimeAtom解决方案感到满意。

What is the best way to solve this problem? 解决此问题的最佳方法是什么?

在vim中,我将使用以下内容:

%s/^\(#.*#\)/#\1#/g

Sublime Text has regex search and replace feature. Sublime Text具有正则表达式搜索和替换功能。 Just press Ctrl + h and search for (#+)([^#]+)(#+) and replace with #$1$2#$3 . 只需按Ctrl + h并搜索(#+)([^#]+)(#+)并替换为#$1$2#$3

在此处输入图片说明

This pattern would only match lines that are heading and leave out any other # character you have in your file. 此模式将仅匹配标题行,并省略文件中包含的任何其他#字符。

这可能对您有用(GNU sed):

sed -i 's/# [^#]* #/#&#/g' file

This is a solution for vim. 这是vim的解决方案。 First you need to start vim giving as args the list of files you want to work on (I suppose here we are talking about all Markdown files in a directory) 首先,您需要启动vim以args形式列出要处理的文件(我想在这里我们正在讨论目录中的所有Markdown文件)

If your shell is zsh 如果您的外壳是zsh

$ vim **/*.md

Otherwise 除此以外

$ vim `find . -type f -name *.md`

Then, inside vim do 然后,在vim里面做

:argdo %s/^#\|#$/##/ge | update

This will replace the starting and the ending '#' with a double '##' in all your files in one single command. 这将在一个命令中用所有文件中的双'##'替换开头和结尾的'#'。 This will work also for headers that don't have the trailing '#'. 这对于没有尾随“#”的标头也适用。 The 'g' flag is needed for the headers that have both leading and trailing '#'. 标头同时带有尾随“#”的标头需要使用“ g”标志。 The 'e' flag tells vim that it's not an error to be unable to find the pattern in a file. 'e'标志告诉vim,无法在文件中找到模式不是错误。 The final 'update' saves the file only if it's changed 最后的“更新”仅在文件更改时保存文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM