如何存储Markdown评论

发布于 2024-08-20 06:23:07 字数 389 浏览 5 评论 0原文

我想使用 Markdown 作为我网站的评论系统,但我偶然发现了以下问题:我应该在数据库中存储什么 - Markdown 中的原始评论、HTML 中解析后的评论,还是两者都存储?

如果用户需要编辑他的评论,我需要 HTML 版本来查看和 Markdown 版本。如果我存储 Markdown 版本,我必须在运行时解析注释。如果我存储 HTML 版本,当用户需要编辑评论时,我需要将评论转换回 Markdown (我发现 Markdownify 为此,但它并不是完美的)。如果我存储两个版本,则使用的空间会增加一倍。

最好的选择是什么?另外,Stack Overflow 如何处理这个问题?

I want to use Markdown for my website's commenting system but I have stumbled upon the following problem: What should I store in the database - the original comment in Markdown, the parsed comment in HTML, or both?

I need the HTML version for viewing and the Markdown version if the user needs to edit his comment. If I store the Markdown version, I have to parse the comments at runtime. If I store the HTML version, I need to convert the comment back to Markdown when the user needs to edit it (I found Markdownify for this but it isn't flawless). If I store both versions, I'm doubling the used space.

What would be the best option? Also, how does Stack Overflow handle this?

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

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

发布评论

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

评论(3

雅心素梦 2024-08-27 06:23:07

存储两者。它违反了数据库规范化的规则,但我认为在这种情况下对于速度优化是值得的 - 解析大量文本是一个非常慢的操作。

你只需要存储它两次,但你可能需要提供它数千次,所以这是一个时空权衡。

Store both. It goes against the rules for database normalization, but I think it's worth it for the speed optimisation in this case - parsing large amounts of text is a very slow operation.

You only need to store it twice, but you might need to serve it thousands of times, so it's a space-time trade-off.

巴黎夜雨 2024-08-27 06:23:07

存储原始 Markdown 并在运行时解析。将转换后的版本存储在数据库中存在一些问题。

  1. 如果用户想要编辑他们的评论,您必须向后转换解析为
  2. 数据库中的原始 Markdown 空间(始终遵循如果不需要存储它,则不要存储的规则)
  3. 对 Markdown 解析器所做的更改必须是对数据库中的每条评论运行,而不是仅在运行时显示。

Store the original markdown and parse at runtime. There are a few problems with storing the converted version in the database.

  1. If user wants to edit their comment, you have to backwards convert parsed into original markdown
  2. Space in database (always go by the rule that if you don't need to store it, don't)
  3. Changes made to markdown parser would have to be run on every comment in the database, instead of just showing up at runtime.
暖心男生 2024-08-27 06:23:07

只需在运行时将 Markdown 渲染为 HTML 即可。

如果您的网站遇到性能问题,Markdown 将是您最后要考虑调整的内容之一。即便如此,我怀疑它是否有意义。

只需看一下 SO 使用的实时 JavaScript 渲染器即可。速度很快。

编辑:
抱歉,我应该说得更清楚。我的意思是只用 PHP 渲染。你会省去很多麻烦——而且你可能还有更重要的事情要担心。

Just render the Markdown to HTML at runtime.

If your site runs into performance issues, the Markdown will be one of the last things you'll look into tweaking. And even then, I doubt it'll make sense.

Just take a look at the realtime JavaScript renderer that SO uses. It's fast.

Edit:
Sorry, I should've been more clear. I meant just render in PHP. You'll save yourself a lot of headache -- and you probably have more important things to worry about.

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