Having raw_html in a system created post

AI-generated summary

The discussion revolves around an issue with raw_html in system-created posts on a Discourse plugin. Luke_Clancy is unable to render raw HTML correctly, as the preprocessor escapes parts of the content, resulting in unintended formatting. The problem is traced to leading spaces and tabs in the HTML string, which are interpreted as Markdown code blocks. simon and codinghorror identify this as the cause, and Luke_Clancy confirms that removing the leading whitespace resolves the issue, though iframe rendering remains problematic, likely due to admin settings.

I am attempting to create system managed posts that utilize raw html on a website plugin (in development). Looking at the documentation and various posts, I think I took the right approach, however the preprocessor still <code>'s out various parts of my post.

This is how it turns out:

This is how it should turn out (I edited in browser):

This is the code I am using to create the post:

post = PostCreator.new(Discourse.system_user,
    title: mmbill.getTitle(),
    raw: cooked,
    cooked: cooked,
    cook_methods: Post.cook_methods[:raw_html],
    archetype: 'regular',
    created_at: createdAt,
    skip_validations: true,
    category: Mmbill.getCatId(),
    is_warning: false,
    meta_data: { mm_type: 'bill', mm_id: final[:bill_id] },
    shared_draft: false,
    topic_opts: {}
 )

Thank you for any help you have to offer!

I’ve run into a similar problem before where the code that creates the raw post content has extra spaces at the beginning of some of its lines. Since the raw output is interpreted as Markdown, extra spaces at the beginning of a line will be considered to be a code block. Depending on how you are creating the raw content, you might be able to fix the problem by reformatting your HTML.

To me, it looks like there are unnecessary leading spaces in those lines. Remove the leading spaces from those lines to fix the issue.

This was it thanks! It is now formatted as written in html. The issue was ruby included the tabs that I used to format my code inside of the string.

It decided to remove the iframe, but I think that has to do with the admin iframe settings.