实现 Stack Overflow 风格评论的想法
我非常喜欢 Stack Overflow 评论 UI,并且正在考虑在我自己的网站上实现同样的功能。 我查看了代码,看起来这里的主要工具是 WMD,带有 JQuery TextArea Resizer 扮演配角。
WMD 在客户端将 Markdown 转换为 HTML。 这非常好,因为它有助于预览,但在将其发送到服务器时我遇到了挑战。 如果存在验证错误(比如用户在评论表单的其他部分输入了无效的电子邮件地址,或者他可能没有输入自己的姓名),那么服务器会通过重新显示带有错误消息的表单来进行响应,并且预填充表单字段。 只是现在评论文本是 HTML,而不是 Markdown,因为服务器甚至从未见过 Markdown。 但我希望它是 Markdown,因为这是用户输入的内容。
这里有什么想法吗?
我考虑过各种想法:
- 进行服务器端 HTML-to-Markdown 转换 。 对这个想法并不那么兴奋。 从 Markdown 转换为 HTML 再次转换为 Markdown 似乎很做作,作为用户,当软件重新格式化我的文本/代码时,我总是觉得很恼火。
- 客户端验证(以增强服务器端验证,我当然会保留它)。 似乎是一个合理的方向,尽管目前我在评论表单上使用 reCAPTCHA,这意味着我需要至少将 reCAPTCHA 部分发布到服务器。
- 丢失 WMD 并使用 MarkdownJ 在服务器上将 Markdown 转换为 HTML。 我需要寻找一些其他机制来完成我想保留的预览功能。
理想情况下,除了 HTML 之外,还有某种方法可以获取文本的 Markdown 版本并将其提交到服务器,但我对 JavaScript 的了解还不够,不知道这是否真的可行。
任何建议表示赞赏。
I like the Stack Overflow comment UI a great deal and I'm looking into implementing the same thing on my own website. I looked at the code and it looks like the main tool here is WMD, with the JQuery TextArea Resizer playing a supporting role.
WMD converts Markdown into HTML on the client side. That's pretty nice because it helps out with previewing but I run into a challenge when sending that to the server. If there's a validation error (say the user entered an invalid e-mail address on some other part of the comment form, or he didn't enter his name maybe), then the server responds by redisplaying the form with an error message and the form fields prepopulated. Only now the comment text is HTML, not Markdown, because the server never even saw the Markdown. But I would like it to be Markdown since that's what the user was entering.
Any ideas here?
I've considered various ideas:
- Do a server-side HTML-to-Markdown transformation. Not that excited about this idea. Seems hokey to transform from Markdown to HTML back to Markdown again, and as a user I always find it irritating when the software reformats my text/code.
- Client-side validation (to augment the server-side validation, which I would of course retain). Seems like a reasonable direction though currently I'm using reCAPTCHA on my comment forms, which means that I need to post at least the reCAPTCHA part to a server.
- Lose WMD and use MarkdownJ to transform the Markdown to HTML on the server. I'd need to look for some other mechanism for accomplishing the preview function, which I want to keep.
Ideally there'd be some way to get at the Markdown version of the text and submit that to the server in addition to the HTML, but I'm not enough of a JavaScript guy to know whether that's a real possibility.
Any suggestions appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅此问题:将 HTML 转换回 Markdown 以在 wmd 中进行编辑< /a> (右侧导航栏上的“相关”框!)。
See this question: Convert HTML back to Markdown for editing in wmd (yay for the "Related" box on the right-hand nav!).
我会将数据作为 markdown 发送,然后让服务器在验证通过后将其转换为 html。 WMD 有一个选项来指定它将发送到服务器的数据格式。 添加
只需在调用 wmd 之前
I would send the data as markdown and then let the server convert it to html when the validations have passed. WMD has an option to specify the format of data it will send to the server. Just add
Before the call to wmd
我只是粗略地了解了 WMD,但将文本区域提交到服务器似乎非常简单 - 事实上,如果文本区域是表单的一部分,我几乎看不出如何避免它。 据我了解,您的文本区域包含标记,WMD 将其转换为 HTML 以在页面的其他部分显示。 只需将文本区域包含在提交的表单中,您就应该在服务器端看到它。
I've only looked at WMD at a cursory level, but submitting the textarea to the server seems pretty straight forward - in fact, I hardly see how you could avoid it if the textarea is part of your form. As I understand it, your textarea contains markup, and WMD converts it to HTML for display in another part of your page. Simply include the textarea in the form that gets submitted and you should see it on the server side.