How to limit some of markdown features for first post?

I want to limit some markdown features for the topic’s first post, which is from my plugin.

It could easily be implemented on the client side by accessing the topicFirstPost attribute of the composer model. However, on the server side in the lib/pretty_text.rb and model/post.rb, I found that only the topic_id is passed to the cook method. I could possibly check the posts_count attribute of the topic, but it won’t work when rebaking.

Perhaps, I shouldn’t use any state in the markdown-it process?

Another way to accomplish this is to generate a placeholder using markdown, then update the preview manually and listen to some cook post-process events on the server side, but that might require some additional work.

I’d prefer the first way if possible. Thanks for any suggestions.

I don’t have a clue, but what do you do with html? It is allowed even markdown would be limited, right?

You’ve written a plugin that generates text that you don’t want to be rendered?

Can you say more about what you’re trying to do?

@Jagster @pfaffman

Please imagine that the plugin adds some custom BBCode tags, or wraps any text with a custom HTML tag for later use, such as decorating it. I don’t want it to take effect outside the first post (or based on other conditions), since I don’t need it and it may cause some side effects.

Also, I may have found a possible solution on the server side. It wraps the cook method of the Post model. I haven’t tested it yet.

class ::Post
  # wrap the original cook method to add features option
  # see the markdown method in lib/pretty_text.rb
  alias_method :original_cook, :cook
  def cook(raw, opts = {})
    features = {
      'feature-name':  is_first_post?
    }

    if opts.has_key?(:features)
      opts[:features].merge!(features)
    else
      opts[:features] = features
    end

    original_cook(raw, opts)
  end
end
4 Likes