如何在 TextMate 中启用 markdown 代码折叠?

发布于 2024-10-15 18:22:06 字数 878 浏览 5 评论 0原文

我想在 textmate 中启用 markdown 的代码折叠。

具体来说,我希望任何标题(前面有一个或多个“#”字符的文本)成为在其与下一个标题和/或水平规则之间切换块的行。示例:

# Level 1 Heading - I'd like the block below to fold

The text I'd like to fold away

## Level 2 Heading

More text...

BBEdit 似乎对于 Markdown 来说做得很好。 TextMate 支持代码折叠,对于用成对(且缩进相同)花括号表示块的语言来说效果很好。

我知道 TextMate 提供了两个可以在语言语法文件中设置的变量,但我需要更多的正则表达式人才才能弄清楚。以下是当前代码折叠设置的 Markdown 语言语法中的内容:

foldingStartMarker = '(?x)
    (<(?i:head|body|table|thead|tbody|tfoot|tr|div|select|fieldset|style|script|ul|ol|form|dl)\b.*?>
    |<!--(?!.*-->)
    |\{\s*($|\?>\s*$|//|/\*(.*\*/\s*$|(?!.*?\*/)))
    )';
foldingStopMarker = '(?x)
    (</(?i:head|body|table|thead|tbody|tfoot|tr|div|select|fieldset|style|script|ul|ol|form|dl)>
    |^\s*-->
    |(^|\s)\}
    )';

非常感谢任何帮助!

I would like to enable code folding for markdown in textmate.

Specifically, I would like any headings (text preceded by one or more '#' characters) to be the line that would toggle blocks between it and the next heading and/or horizontal rule. Example:

# Level 1 Heading - I'd like the block below to fold

The text I'd like to fold away

## Level 2 Heading

More text...

BBEdit seems to do this just fine for markdown. TextMate supports code folding just fine for languages that denote blocks with paired (and identically indented) curly braces.

I know TextMate offers two variables you can set in the language grammar file, but I'd need more talent with regular expressions than I possess to figure it out. Following is what currently resides in the markdown language grammar for the code folding settings:

foldingStartMarker = '(?x)
    (<(?i:head|body|table|thead|tbody|tfoot|tr|div|select|fieldset|style|script|ul|ol|form|dl)\b.*?>
    |<!--(?!.*-->)
    |\{\s*($|\?>\s*$|//|/\*(.*\*/\s*$|(?!.*?\*/)))
    )';
foldingStopMarker = '(?x)
    (</(?i:head|body|table|thead|tbody|tfoot|tr|div|select|fieldset|style|script|ul|ol|form|dl)>
    |^\s*-->
    |(^|\s)\}
    )';

Any help greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

王权女流氓 2024-10-22 18:22:06

TextMate 根本无法做到这一点。

TextMate 的语言解析器逐行读取文件。这意味着为了折叠代码,需要有一个开始标记和一个结束标记。如

。在这种情况下,由于除了下一节的开头之外,节的结尾没有被任何其他内容标记,因此逐行解析器无法返回并说空行应该标记代码折叠块的结尾。

这有道理吗?

TextMate flat-out can't do this.

TextMate's language parser reads files on a line-by-line basis. Which means that in order for code to be folded there needs to be a start marker AND an end marker. Like <ul> and </ul>. In this case, since the end of a section is not marked by anything other than the start of the next section, the line-by-line parser cannot go back and say an empty line should mark the end of a code folded block.

Does that make sense?

动听の歌 2024-10-22 18:22:06

Macromates 博客 上,他们解释了如何为 Markdown 启用基本代码折叠。只要您在标题中使用“#”符号,就可以在标题级别折叠。

对于 Markdown 的折叠设置(范围为 text.html.markdown),让我们看看一些更隐蔽的模式:

<代码>
{foldingIndentedBlockStart = '^#+\s+';
FoldingIndentedBlockIgnore = '^(?!#+\s+)';
}

我们让标题作为开始标记,然后我们有效地忽略不是标题的所有内容,导致这些行尽管没有缩进,但仍包含在可折叠块中。这允许您将 Markdown 文档的整个部分折叠到仅标题。

On the Macromates blog, they explain how to enable basic code folding for markdown. This lets you fold at the header level so long as you're using the "#" symbol for your headers.

For Markdown's folding settings (scoped to text.html.markdown), let's look at some more sneaky patterns:


{ foldingIndentedBlockStart = '^#+\s+';
foldingIndentedBlockIgnore = '^(?!#+\s+)';
}

We let a heading be a start marker and then we effectively ignore everything which is not a heading, causing those lines to be included in the foldable block despite not being indented. This allows you to collapse entire sections of a Markdown document down to just the headings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文