docpad / docpad-plugin-text

Allows you to render templateData with HTML elements

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rendering in attributes

marisks opened this issue · comments

I am wondering if it is possible to render output in HTML attributes and stripping out the output HTML by rendering just text.

I am using "description" field of the page meta-data to render HTML's meta tag for description (in Jade).

meta(name="description", content="#{getPreparedDescription()}")

and the description meta-data value looks like this:

description: >
  <t render="markdown">
  Some text with [link](/2017/01/22/article/).
  </t>

Now the description value in the "meta" tag is just escaped and rendered as is. Is there any function available to leave only the text by stripping out <t> tag and Markdown?

The text plugin in theory should render anywhere. But I'd imagine that jade may do some encoding business on the meta attribute.

If the text plugin did work, you'd still have:

meta(name="description", content="#{getPreparedDescription()}")

converted to:

meta(name="description", content="Some text with <a href='/2017/01/22/article/'>link</a>")

whereas you probably want:

meta(name="description", content="Some text with link")

As the meta description is generally what is used on google as the content underneath your site title in a search result. So links won't render.

I'd have two descriptions - one that is just text, and one that is HTML.

Two descriptions will work but it is harder to maintain - have to copy paste same text and strip tags/markdown manually. It would be nice to have some function which would do that.

Can I use some library inside docpad.coffee like https://github.com/wooorm/strip-markdown? Then I could do it myself inside the getPreparedDescription function.

Solved this by installing https://github.com/wooorm/strip-markdown
Then in the docpad.coffee:

strip = require("strip-markdown")
remark = require("remark")

strip_md = (value) ->
  remark().use(strip).processSync(value).toString().trim()

And in the getPreparedDescription method:

getPreparedDescription: ->
    strip_md(@document.description) or @site.description