Reviewing pages in EPiServer – identifying commentable areas

(If this post seems out of context, return to introduction post and hopefully it will make more sense)

How to identify commentable areas

As described previously an article is made up of both an article page type as well as one more more chapter page types. The chapter page contains a xhtml editor in which editors are able to create headers of type h3. The challenge for us was that comments needed to be placed on this level. Comments on the article as a whole would not pose a problem since they can easily be connected to the article itself (and it’s page guid). The xhtml property, or rather it’s sections, are harder to identify since it can consist of any number of H3 tags and there is no natural way of identifying them individually. Or well, one natural way would be to use the actual text (in some way) in the H3 but since editors could change this at a moments notice it felt like a brittle at best solution.

Adding some identifiers

We realized we needed to add identifiers to each H3 tag to be able to identify to which section a specific comment belongs.

Generating the identifier

When a page that is up for review is published we have a DataFactory event that initiate the generation of thread ids on the page in question. This follows the following process

  • Find all properties on the page type that are marked with the attribute GenerateThreadIdForReview. This is a marker attribute that just informs that the property should have id’s generated for it
  • Parse the text in all the found properties and then add a html attribute data-thread-id to all H3 tags that doesn’t already have the attribute. The value of the attribute is set to a Guid generated in the process
  • Save the page

Or in more HTML words, this

<!-- before -->
<h3>Some title</h3>
<p>This is some text</p>

<!-- after publish -->
<h3 data-thread-id="CA51BA9B-85DD-47D2-81B5-81BCDE7E36A9">Some title</h3>
<p>This is some text</p>

Tiny MCE

With the problem of having identifiers on specific H3 out of the way we quickly realized that we need to tweak certain aspects of the Tiny MCE-editor. For instance, if the editor copies a H3 and paste it we would have duplicated identifiers, which is not good. There are a few other minor edge cases needed to consider but the copy/pasting of H3s was the big one.