javascript 对决,markdown 无法正确解析

发布于 2024-08-06 07:58:55 字数 873 浏览 4 评论 0原文

我有一个令人困惑的问题。

我有两个文件,都运行摊牌。然而,似乎只有一个文件能够正确解析。

事情是这样的。

文件 1 通过 PHP 和 AJAX 运行。

文件 2 通过 AJAX 请求获取。处理ajax请求的文件包含此代码。

这是 ajax

如您所见,将详细信息显示到屏幕上的 jQuery 代码完全相同来自变量和 JSON 字符串,而不是来自数据库的直接字符串。

现在,文件 #1 无法正确解析 markdown,而文件 2 则可以...

这是文件 #1 和 #2 的屏幕截图

文件 #1

文件一不起作用

文件 #2

文件两个工作

知道可能是什么原因造成的吗?另外,有什么修复的想法吗?

I have a confusing problem.

I have two files, both that run showdown. However, it seems that only one file parses correctly.

Here's the deal.

File 1 is run through PHP and AJAX.

File 2 is grabbed via an AJAX request. The file that processes the ajax request contains this code.

Here is the ajax

As you can see, the jQuery code that puts the details onto the screen are exactly the same apart from the variables and the JSON string instead of a direct string from the database.

Now file #1 doesn't parse the markdown correctly, where file two does...

Here is a screenshot of files #1 and #2

File #1

file one not working

File #2

file two working

Any idea on what could possibly be causing this? Also, any ideas on a fix?

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

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

发布评论

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

评论(2

风吹过旳痕迹 2024-08-13 07:58:55

也许是因为在文件 2 中你有:

  $copy = preg_replace('^(.*)\n(.*)^', '$1<br />$2', $result['copy']);
  $copy = preg_replace('/[\r]+/', '$1<br />$2', $result['copy']);

它忽略了第一个 preg_replace()。您想要:

  $copy = preg_replace('^(.*)\n(.*)^', '$1<br />$2', $result['copy']);
  $copy = preg_replace('/[\r]+/', '$1<br />$2', $copy);

您已在文件 1 中正确实现了该功能。这可能意味着文件 1 的行为是正确的,而文件 2 的行为实际上是不正确的,具体取决于您看待它的方式。

编辑

回答您的评论:

替换(在文件1中):

   $copy = preg_replace('^(.*)\n(.*)^', '$1<br />$2', $js_r->copy);
   $copy = preg_replace('/[\r]+/', '$1<br />$2', $copy);

   $copy = preg_replace('/[\r]+/', '$1<br />$2', $js_r->copy);

这是否达到了预期的效果?

Maybe it is because in file 2 you have:

  $copy = preg_replace('^(.*)\n(.*)^', '$1<br />$2', $result['copy']);
  $copy = preg_replace('/[\r]+/', '$1<br />$2', $result['copy']);

Which ignores the first preg_replace(). You want:

  $copy = preg_replace('^(.*)\n(.*)^', '$1<br />$2', $result['copy']);
  $copy = preg_replace('/[\r]+/', '$1<br />$2', $copy);

Which you have correctly implemented in file 1. This could mean that file 1's behaviour is correct and file 2's behaviour is actually incorrect, depending on the way you look at it.

EDIT

To answer your comment:

Replace (in file 1):

   $copy = preg_replace('^(.*)\n(.*)^', '$1<br />$2', $js_r->copy);
   $copy = preg_replace('/[\r]+/', '$1<br />$2', $copy);

With

   $copy = preg_replace('/[\r]+/', '$1<br />$2', $js_r->copy);

Does that achieve the desired effect?

内心旳酸楚 2024-08-13 07:58:55

好吧,

看起来是文件中换行符的解析...但是我们已经将其从
更改为,因为这导致降价解析错误地双重转义的问题,

所以它现在读取

$copy = preg_replace("/\n/", "\\\\n", $js_r->copy);
$copy = preg_replace("/\r/", "\\\\r", $copy);

哪个工作正常并且有效地解析两个文件

Ok,

So it seems it was the parsing of the line breaks in the file... However we have changed it from
as this is causing the issue of the markdown parsing incorrectly to double escaped

so it now reads

$copy = preg_replace("/\n/", "\\\\n", $js_r->copy);
$copy = preg_replace("/\r/", "\\\\r", $copy);

which works correctly and parses both files efficiently

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