Ideas on implementing Stack Overflow-style comments
Asked Answered
I

3

7

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.

Impassable answered 27/12, 2008 at 18:57 Comment(0)
C
5

See this question: Convert HTML back to Markdown for editing in wmd (yay for the "Related" box on the right-hand nav!).

Congenital answered 27/12, 2008 at 20:35 Comment(1)
Thanks Chris. This is exactly what I was looking for. This time bitten by RTFR I guess.Impassable
I
5

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

wmd_options = {
        //Markdown or HTML
        output: "Markdown"
    };

Before the call to wmd

Inaudible answered 28/12, 2008 at 8:3 Comment(0)
A
3

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.

Anagnorisis answered 27/12, 2008 at 19:20 Comment(2)
Yes, we retrieve and store the "raw" text a user enters here on SO - exactly the way you describe!Beefwood
Yeah, I have it submitting the textarea just fine...it's just that the browser is sending HTML to the server, not Markdown. But it looks like from Chris' response one of the WMD options is to send to the server as Markdown. Sigh, bitten by RTFM. :-PImpassable

© 2022 - 2024 — McMap. All rights reserved.