我正在从头开始构建 WYSIWYG 作为学术练习,但我遇到了障碍。我正在尝试构建 WYSIWYG 以使用一些基本的 Markdown 方法工作,例如使用星号表示粗体/斜体,使用 haskmarks 表示标题等。但是,我遇到了块引用的问题。这是我目前用来处理块引用输入的代码:
$content = '$_POST['content'];
while (preg_match('^>\s(.*)$', $content)) {
$content = preg_replace('^>\s(.*)$', '<blockquote>$1</blockquote>', $content);
};
基本上,它会查找以“大于”符号开头的任何行,提取文本并将其放在块引用标签中,如下所示:
input:
> this is a blockquote.
output:
<blockquote>this is a blockquote.</blockquote>
这很好,但 Markdown 也可以采用多行块引用并将其转换为单个块引用。例如:
input:
> this is a blockquote that
> i decided to separate across
> several lines.
output:
<blockquote>this is a blockquote that i decided to separate across several lines.</blockquote>
我想模仿该功能,但使用我当前的代码,我最终会得到这个:
output:
<blockquote>this is a blockquote that</blockquote><blockquote>i decided to separate across</blockquote><blockquote>several lines.</blockquote>
我只是不确定如何正确连接块引用。</blockquote><blockquote>
我想到的一种方法是更改每一行,然后在它们之间没有双换行符的情况下进行新的搜索,但这似乎效率低下。代码将变为:
$content = '$_POST['content'];
while (preg_match('^>\s(.*)$', $content)) {
$matched = true;
$content = preg_replace('^>\s(.*)$', '<blockquote>$1</blockquote>', $content);
};
if ($matched) {
$content = preg_replace('</blockquote>(\n|\r)?<blockquote>', '', $content);
};
我想这会奏效,但我觉得有一种更好的方法可以利用正则表达式来预测并获取所有额外的行。不幸的是,我不知道那会是什么。