Closed dazinator closed 7 years ago
To add some more info, I added this script to my theme scripts folder:
var rExcerpt = /<!-- ?more ?-->/;
console.log(" excerpt script fired")
hexo.extend.filter.register('after_post_render', function(data) {
var content = data.content;
if (rExcerpt.test(content)){
data.content = content.replace(rExcerpt, function(match, index){
data.excerpt = content.substring(0, index).trim();
data.more = content.substring(index + match.length).trim();
console.log(" excerpt: " + data.excerpt);
console.log(" more: " + data.more);
return '<a id="more"></a>';
});
} else {
console.log(" no excertp found: " + content);
data.excerpt = '';
data.more = content;
}
});
When it runs, the regex doesn't match, as the content contains this:
<p><!--more--></p>
Could this be the reason? i.e it looks like the read more directive is getting html encoded.
I have tried switching back to the default markdown renderer and the issue remains. Would appreciate any help or pointers around how to get excerpts working. Even if someone has a vanilla example that would be really useful.
I haven't written any theme before. But based on my understanding, I think it should work by default.
https://github.com/hexojs/hexo/blob/master/lib/plugins/filter/after_post_render/excerpt.js#L3
It seems that the hexo-renderer-markdown-it
plugin html encodes the <
and >
characters in the comment as you observed, which prevents it from being recognized when the excerpt is being populated. I was able to get it working by wrapping the comment in an escape
block like this:
<escape><!-- more --></escape>
That seems to prevent the rendering plugin from html encoding the comment and excerpts start working again as expected. This may or may not be a bug in the renderer, I'm not sure.
The official markdown spec does not address html comments specifically, but it allows for both the use of <
as a literal character (to be escaped automatically) as well as the direct embedding of inline html. It's up to the renderer to recognize under which circumstance it's being used and to either escape or not escape it appropriately. I'd argue that html comments should be treated as inline html and embedded without escaping, but I suppose it could be argued the other way as well.
Environment Info
Node version(
node -v
)Your site
_config.yml
Your theme
_config.yml
Plugin version(
npm ls --depth 0
)For BUG
For question
I have migrated posts from octopress, and they contain the following which is meant to render a "Read More" link:
However hexo is displaying the whole blog post, and it isn't rendering the post as an excerpt, and not converting this to a "Read More" link. So the post displays like this:
This is a brand new / vanilla setup of hexo, except that I did also install a different markdown plugin: https://github.com/celsomiranda/hexo-renderer-markdown-it
Am I doing something wrong perhaps?
For feature request
just push feature request