开发者

Extending Jekyll and Liquid to parse post's content

My blog, powerred by Jekyll, serves out a Atom feed.

---
layout: nill
rooturi: http://stefan.artspace44.com
---

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

...

{% for post in site.posts %}
  <entry>
    <title>{{ post.title }}</title>
    <link href="{{ page.rooturi }}{{ post.url }}" />
    <updated>{{post.date | date_to_xmlschema }}</updated>
    <id>{{ page.rooturi }}{{ post.id }}</id>
    <content type="html">{{ post.content | xml_escape }}</content>
  </entry>
 {% endfor %}
</feed>

I need to change the content of each post, so that

<img href="/images/01.jpg" />
<a href="/2010/post/">Post</a>

be开发者_运维技巧comes:

<img href="http://stefan.artspace44.com/images/01.jpg" />
<a href="http://stefan.artspace44.com/2010/post/">Post</a>

I was thinking of doing something along the lines of

<content type='html'>
  {{ post.content | make_hrefs_base page.rooturi }}
</content>

Where would I code this in jekyll or liquid, and how could I solve the problem of changing only the href values that point to "/" and not "http://otherdomain.com/"?

Thank you


Where would I code this in jekyll or liquid?

In the recently-released Jekyll 0.6.0, you can create your own plugins, including Liquid tag plugins. You can check out the Jekyll plugin documentation for more info, but that would be your best bet.

How could I solve the problem of changing only the href values that point to "/" and not "http://otherdomain.com/"?

Seems pretty easy. In your custom Liquid tag, check to see if the first character is a '/'; if it is, then prepend your new domain. You could probably use a Ruby HTML parser to find all instances of <a>, and then change the href attributes as appropriate.


I had the same problem in the feed of my blog, and I managed to solve it without using a plugin, i.e. only with vanilla Liquid.

In my Atom XML file, my content is populated like this:

<content type="html">
    {{ post.content | replace: site.feed_linkurl_find, site.feed_linkurl_replace | replace: site.feed_imgurl_find, site.feed_imgurl_replace | xml_escape }}
</content>

...and I have these variables in my config file:

# URL replacements for feeds
feed_linkurl_find: href="/
feed_linkurl_replace: href="http://christianspecht.de/
feed_imgurl_find: src="/
feed_imgurl_replace: src="http://christianspecht.de/

In other words, I just do two ordinary string replaces, one for links and one for images.

The trick is:
In both cases, I replace href="/ by href="http://christianspecht.de/, so only those links that actually begin with / are affected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜