# Importing posts with Markdown

**URL:** https://meta.discourse.org/t/importing-posts-with-markdown/282635
**Category:** Migration
**Created:** [October 18, 2023, 12:09pm UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635 "2023-10-18T12:09:55Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![n1bff](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@n1bff](https://meta.discourse.org/u/n1bff)
#### Post date: [October 18, 2023, 12:09pm UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/1 "2023-10-18T12:09:55Z")

</div>

I rewrote [discourse/script/import\_scripts/json\_generic.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/main/script/import_scripts/json_generic.rb) a bit for my own data. It works well but I cannot figure out how to have Discourse use Markdown in the imported posts.

For example a post might be  
` **some text here in bold** and something else here`  
After importing Discourse shows it verbatim like that, not using the `**` Markdown syntax to bold the “some text here in bold” part. And weirdly also if I try to edit such a post using the normal Discourse GUI, it will ignore any formatting.

The import script uses  
`cook_method: Post.cook_methods[:raw_html],`  
I also tried  
`cook_method: Post.cook_methods[:regular],`  
without any visible change.

How can I make Discourse recognise Markdown when importing data?

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [October 18, 2023, 12:42pm UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/2 "2023-10-18T12:42:44Z")

</div>

> [@n1bff](#):
>
> And weirdly also if I try to edit such a post using the normal Discourse GUI, it will ignore any formatting.

Even if you add formatted text at the end of the post, this text won’t be formatted?

Does `Rebuild HTML` do something?

---

<div class="post-metadata">

### Author: ![n1bff](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@n1bff](https://meta.discourse.org/u/n1bff)
#### Post date: [October 18, 2023, 4:06pm UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/3 "2023-10-18T16:06:13Z")

</div>

> [@Arkshine](#):
>
> Even if you add formatted text at the end of the post, this text won’t be formatted?

Yes, it’s super weird. The preview looks correct but if I save the edit, the unformatted post is shown.

`Rebuild HTML` does not fix it.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [October 18, 2023, 5:10pm UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/4 "2023-10-18T17:10:54Z")

</div>

> [@n1bff](#):
>
> `Rebuild HTML` does not fix it.

What does the raw look like in the database, can you check it with [data explorer](https://meta.discourse.org/t/32566?silent=true)? Or output in the rails console.

Can you ensure that the rebaking job is executed in Sidekiq when you trigger Rebuild HTML?

Or rebake with the rails console to see if there’s some error?

Those are just random suggestions 🤷 It looks like the post isn’t cooked.

---

<div class="post-metadata">

### Author: ![n1bff](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@n1bff](https://meta.discourse.org/u/n1bff)
#### Post date: [October 18, 2023, 5:29pm UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/5 "2023-10-18T17:29:28Z")

</div>

Thank you!

> [@Canapin](#):
>
> What does the raw look like in the database, can you check it with [data explorer](https://meta.discourse.org/t/32566)? Or output in the rails console.

Using `SELECT raw=cooked FROM posts WHERE ...` returns true for the post.

> [@Canapin](#):
>
> Can you ensure that the rebaking job is executed in Sidekiq when you trigger Rebuild HTML?

I have no idea what that means or how I can check that 😃 Could you give me a pointer?

> [@Canapin](#):
>
> Or rebake with the rails console to see if there’s some error?

I ran `bundle exec rake posts:rebake` and the post is the same as before and behaves the same as before on editing.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [October 19, 2023, 9:58am UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/6 "2023-10-19T09:58:17Z")

</div>

> [@n1bff](#):
>
> I have no idea what that means or how I can check that 😃 Could you give me a pointer?

Forget about that, that won’t help after all.

I’d create a new post in which I’d paste the same markdown content as one of the unformatted posts.

I’d expect the new post to be properly formatted by Discourse.

Then, I’d compare the `raw` field of both posts; either with [data explorer](https://meta.discourse.org/t/32566?silent=true) or the rails console.

If one is formatted when cooked, and the other one isn’t when you rebuild HTML (which rebakes the post), there _should_ be a difference in their raw. 🤔

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 19, 2023, 9:58am UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/7 "2023-10-19T09:58:45Z")

</div>

What happens if you edit the post? Does it look like you expect? Does it render correctly if your edit and save?

---

<div class="post-metadata">

### Author: ![n1bff](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@n1bff](https://meta.discourse.org/u/n1bff)
#### Post date: [October 19, 2023, 11:47am UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/8 "2023-10-19T11:47:06Z")

</div>

I took one of the offending posts, went into its edit mode, copied the contents and made a new posts with it. The new posts uses the Markdown formatting, I see bold text and a link with a title.

In the database their `raw` is identical but their `cooked` is not.

`SELECT (SELECT raw FROM posts WHERE id = 13846) = (SELECT raw FROM posts WHERE id = 13735);` → `t`

`SELECT (SELECT cooked FROM posts WHERE id = 13846) = (SELECT cooked FROM posts WHERE id = 13735);` → `f`

Clicking “Rebuild HTML” on the badly formatted posts does no change.

`/logs/` shows nothing at this time (only older warnings and errors).

---

<div class="post-metadata">

### Author: ![n1bff](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@n1bff](https://meta.discourse.org/u/n1bff)
#### Post date: [October 19, 2023, 11:48am UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/9 "2023-10-19T11:48:22Z")

</div>

When I edit it, the preview looks correct, the Markdown formatting is applied there (e.g. “ **foo** ”). Once I save, the post will still show the formatting characters (e.g. “` **foo** `”)

---

<div class="post-metadata">

### Author: ![renato](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/renato/32/383632_2.png) [@renato](https://meta.discourse.org/u/renato)
#### Post date: [October 19, 2023, 12:21pm UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/10 "2023-10-19T12:21:17Z")

</div>

Can you try running

```ruby
Post.update_all(cook_method: Post.cook_methods[:regular])

```

in the Rails console and then “Rebuild HTML” again?

Note that it will update **all your posts** to use the regular cooking method.

---

<div class="post-metadata">

### Author: ![n1bff](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@n1bff](https://meta.discourse.org/u/n1bff)
#### Post date: [October 19, 2023, 2:32pm UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/12 "2023-10-19T14:32:39Z")

</div>

```plaintext
/var/www/discourse$ bundle exec rails c
Loading production environment (Rails 7.0.7)
irb(main):001:0> Post.update_all(cook_method: Post.cook_methods[:regular])
=> 13809
irb(main):002:0>

```

Done! Sadly no change.

* * *

But I solved it. And I am sorry for not mentioning what plugins I had installed as they can interfere…

I have the [Topic Ratings Plugin](https://meta.discourse.org/t/topic-ratings-plugin/39578/1333579) installed and enabled for the category into which my imports go.

On a whim I disabled the plugin and tried again to edit a “broken” post. Now upon saving, the Markdown formatting was used for fancy rendering. I re-enabled the plugin and tried again with success. I imported new posts and it also renders the posts correctly now (using `cook_method: Post.cook_methods[:regular]`).

I have no idea what is/was going on there but it seems to have been **fixed by toggling the Topic Ratings Plugin [off and on](https://www.youtube.com/watch?v=t2F1rFmyQmY) again**.

Thank you all for the great suggestions and problem hunting!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [November 18, 2023, 2:33pm UTC](https://meta.discourse.org/t/importing-posts-with-markdown/282635/13 "2023-11-18T14:33:07Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
