Processing an entire post?

(NomNuggetNom ) #1

The Discourse.Dialect.postProcessText() handle is called for every line in a post. Is there any handle that provides the entire post, perhaps as an array of text lines?

(Robin Ward) #3

My first question is why? What do you need to post process that is not done one line at a time?

(NomNuggetNom ) #4

I’m experimenting with writing a plugin for tables. I need to be able to be aware of what the first line was, and, more importantly, the next line. Here is a standard format for a table:

Header | Header
Value  |  Value

For the headers, I need to be able to detect that the next line is a bar, and give them the appropriate format.

(Robin Ward) #5

That’s going to be an ambitious plugin to write :smile: Ideally it needs to be written as a Discourse.Dialect which would be very complicated as it has multiple lines and states.

There is currently no API to get at the final string, but I wouldn’t be against adding one if you want to submit a PR similar to addPreProcessor called addPostProcessor that would run after everything happened.

(NomNuggetNom ) #6

That would be perfect! However I don’t think I’ll be able to pull that off - I don’t know how Discourse works well enough, and I’m no master at JS yet ;). If you or someone else ends up doing it, I would love to see how it works so that I can learn :).

If you want to give me some pointers I’ll do my best.

(Jens Maier) #7

Getting started with Discourse’s markdown parser is Hell. Capital H.

The long and boring way that, fortunately, doesn’t require much preexisting knowledge about the codebase or even about JavaScript, is to run Discourse in development mode, open it up in Chrome, open Chrome’s dev tools and place a breakpoint on line 200 in discourse/dialects/dialect.js and step through the code as it – simply start composing a post and type anything.

This file defines the Discourse-specific Markdown dialect as well as a bunch of helper functions to process bbcode-style block tags and the like, which are defined in the other files in the same folder. The Markdown parser itself lives in assets/better_markdown.js, and it contains all of the Gruber-style markdown handlers.

You could theoretically write your table parser as a pre-processor that itself calls the parser to render each cell to HTML.

By the way, be prepared to do a lot of beancounting and manual string searching and splitting. JS’s regex parser has less features than a particularly uninteresting rock; most importantly, recursive pattern re-execution and certain assertions are just missing.

(NomNuggetNom ) #8

Wow, I had no idea you could use Chrome’s debugger like that, that’s awesome! Thank you! :smiley:.

I already wrote a working prototype parser, hopefully I can just integrate it straight into Discourse :).

EDIT: Is there any way to run Discourse in dev mode without running it locally?

(Jens Maier) #9

If it’s local or not technically doesn’t matter, but you cannot use the Docker install method since the launcher doesn’t install the development dependencies.

As you can probably see in my screenshot, I’m working on a Windows PC, but the Discourse instance that’s open in the debugger is running on a Linux box in the basement. The dev server (rails s) is running in a PuTTY window in the background while my editor is running on Windows, accessing files via a mounted Samba share.

If you don’t already have a dedicated server in your household (that Linux box is also my mailserver, router, firewall, VCR…), you can always set up a virtual machine on your main computer and install the latest Ubuntu server with Ruby, PostgreSQL, Redis and Samba. I tweaked Postgres’ auth settings a bit to give my default user superuser access, but I don’t think there’s any more to be done in terms of system setup. Altho, if you’re going with Ubuntu, you may need to install gcc and a bunch of libraries and -dev packages to successfully compile certain gems’ native extensions.

Everything else can be done without root privileges, i.e. clone Discourse, gem install bundler, bundle install – but I’m probably being optimistic. :slight_smile: