3

如何将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 突出显示不同级别标题的方式。)

4

1 回答 1

4

将此函数放在您的 vimrc 中,并根据需要使用:call VO2MD()or 。: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
于 2012-03-23T16:01:18.067 回答