gpt4 book ai didi

regex - 如何从 VimOutliner 转换为 Markdown?

转载 作者:行者123 更新时间:2023-12-01 12:55:56 25 4
gpt4 key购买 nike

我如何转换 VimOutliner文件到 Markdown?换句话说,我如何像这样转动制表符内嵌的轮廓...

Heading 1
Heading 2
Heading 3
: Body text is separated by colons.
: Another line of body text.
Heading 4

...到像这样的空行分隔的散列式标题:

# Heading 1

## Heading 2

### Heading 3

Body text.

## Heading 4

我尝试过定义一个宏,但我对 Vim 还很陌生(而不是编码员),所以到目前为止我一直没有成功。感谢您的帮助!

(PS -- 至于 Markdown,我确实知道很棒的 VOoM 插件,但我仍然更喜欢为看不到散列字符的文档做初始大纲。另外,我也喜欢 VimOutliner 突出显示不同级别的方式标题。)

最佳答案

将这个函数放在你的 vimrc 中,然后根据需要使用 :call VO2MD():call MD2VO()

function! VO2MD()
let lines = []
let was_body = 0
for line in getline(1,'$')
if line =~ '^\t*[^:\t]'
let indent_level = len(matchstr(line, '^\t*'))
if was_body " <= remove this line to have body lines separated
call add(lines, '')
endif " <= remove this line to have body lines separated
call add(lines, substitute(line, '^\(\t*\)\([^:\t].*\)', '\=repeat("#", indent_level + 1)." ".submatch(2)', ''))
call add(lines, '')
let was_body = 0
else
call add(lines, substitute(line, '^\t*: ', '', ''))
let was_body = 1
endif
endfor
silent %d _
call setline(1, lines)
endfunction

function! MD2VO()
let lines = []
for line in getline(1,'$')
if line =~ '^\s*$'
continue
endif
if line =~ '^#\+'
let indent_level = len(matchstr(line, '^#\+')) - 1
call add(lines, substitute(line, '^#\(#*\) ', repeat("\<Tab>", indent_level), ''))
else
call add(lines, substitute(line, '^', repeat("\<Tab>", indent_level) . ': ', ''))
endif
endfor
silent %d _
call setline(1, lines)
endfunction

关于regex - 如何从 VimOutliner 转换为 Markdown?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9840644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com