Article/Blog-Post Blurb/Preview

Recently I added a simple blog to my website. Of course, there needs to be an entry point into each post and the obvious way to do this is have a list of recent blog posts with a small preview of each one.

At first I merely truncated the article to the first few hundred characters. It soon became apparent that this wasn't ideal as the posts contained HTML which was often truncated mid-tag which left some tags open, ruining the rendering of the page.

I toyed with trying to keep track of which tags had been opened and then adding them back in after the truncation. This seemed overly complicated and in some cases led to strange side effects.

In the end I decided that the simplest thing to do which also lead to the best results was to strip all tags before doing the truncation. The one downside to this is that I need to make sure that the first few hundred characters in all of the posts are plaintext and don’t contain any special formatting such as headings, lists or code examples.

The way around this would be to add an extra field when creating a blog post for the preview. This could be limited to a certain number of characters but would allow the use of HTML in the preview and keep sane formatting. But for the meantime, the striptags method will suffice.