forked from theme-next/hexo-theme-next
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to Nunjucks [3] (theme-next#1226)
- Loading branch information
1 parent
b8eeeed
commit 0a3f3c3
Showing
13 changed files
with
108 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* global hexo */ | ||
|
||
'use strict'; | ||
|
||
hexo.extend.filter.register('after_post_render', data => { | ||
const { config } = hexo; | ||
const theme = hexo.theme.config; | ||
const filters = { | ||
excerpt : theme.auto_excerpt && theme.auto_excerpt.enable && data.excerpt === '', | ||
exturl : theme.exturl, | ||
lazyload: theme.lazyload | ||
}; | ||
if (!filters.excerpt && !filters.exturl && !filters.lazyload) { | ||
return; | ||
} | ||
const cheerio = require('cheerio'); | ||
const $ = cheerio.load(data.content, { | ||
decodeEntities: false | ||
}); | ||
if (filters.lazyload) { | ||
$('img').each((i, o) => { | ||
let src = $(o).attr('src'); | ||
$(o).attr('data-src', src).removeAttr('src'); | ||
}); | ||
} | ||
if (filters.exturl) { | ||
const url = require('url'); | ||
var siteHost = url.parse(config.url).hostname || config.url; | ||
|
||
$('a').each((i, o) => { | ||
var href = $(o).attr('href'); | ||
// Exit if the href attribute doesn't exists. | ||
if (!href) return; | ||
|
||
var data = url.parse(href); | ||
|
||
// Exit if the url has same host with `config.url`, which means it's an internal link | ||
if (!data.protocol || data.hostname === siteHost) return; | ||
|
||
// If title atribute filled, set it as title; if not, set url as title. | ||
var title = $(o).attr('title') || href; | ||
|
||
var encoded = Buffer.from(href).toString('base64'); | ||
|
||
$(o).replaceWith(() => { | ||
return $(`<span class="exturl" data-url="${encoded}" title="${title}">${$(o).html()}<i class="fa fa-external-link"></i></span>`); | ||
}); | ||
}); | ||
} | ||
if (filters.excerpt) { | ||
const elements = $.root().children(); | ||
const _$ = cheerio.load('', { | ||
decodeEntities: false | ||
}); | ||
var length = 0; | ||
elements.each((i, o) => { | ||
if (length > theme.auto_excerpt.length) { | ||
return; | ||
} | ||
length += $(o).text().length; | ||
_$.root().append($(o).remove()); | ||
}); | ||
|
||
if ($.root().children().length) { | ||
data.excerpt = _$.html(); | ||
data.more = $.html(); | ||
data.content = data.excerpt + '<a id="more"></a>' + data.more; | ||
} else { | ||
data.excerpt = ''; | ||
data.content = _$.html(); | ||
data.more = data.content; | ||
} | ||
} else { | ||
data.content = $.html(); | ||
} | ||
|
||
}, 20); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
/** | ||
* next-url.js | https://theme-next.org/api/helpers/next-url/ | ||
*/ | ||
|
||
/* global hexo */ | ||
|
||
'use strict'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters