Include any files to Hexo Post

include any local files with syntax highlighter

1
{% include_file [title:'scoped title'] [render:[true|false]] [lang:language] [from:line] [to:line] [pretext:[true|false]] path/to/file %}{% endinclude_file %}
option key description default
title caption title path.extname(pathToFile)
pretext wrap codes with <pre/><code/> and syntax highlight enable indicator true
lang syntax highlighter for spesific language, needs pretext to true empty string by default treat as text plain
from embed start line 0
to embed ends line Number.MAX_VALUE
render Render the file to include, requiring the lang option parameter

More Samples

path relative to source directory

1
{% include_file 'fixtures/include-one.txt' %}{% endinclude_file %}
include-one.txtview raw
1
first included

path relative to current file (with space and without preText)

1
{% include_file '../fixtures/include two.txt' pretext:false %}{% endinclude_file %}
second included

include_code alias

Inserts code snippets relative to source folder. code_dir option in the config also be used for reference finder.

references:

Embed the whole content of test.js

test.js inside source/downloads/code (hexo.code_dir)

1
{% include_file lang:javascript 'test.js' %}{% endinclude_file %}
test.jsview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// global variables
const toMatch = new Date('2022-12-01T09:30:20');
let data = `date to match: ${toMatch}<br/>`;
document.getElementById('variables').innerHTML = data;

// method 1 using timestamp

const yourDate = toMatch.getTime(); //Get the timestamp
const today = Date.now();
// 2 days in millisecond
const day = 60 * 60 * 1000 * 24 * 2;
data = `
today: ${today}<br/>
yourDate: ${yourDate}<br/>
2 days in millisecond: ${day}<br/>
today - yourDate: ${today - yourDate}<br/>
is greater than 2 days: ${today - yourDate > day ? '<span class=green>true</span>' : '<span class=red>false</span>'}`;
document.getElementById('timestamp').innerHTML = data;

// method 2 using hours
const now = new Date();

const msBetweenDates = Math.abs(toMatch.getTime() - now.getTime());

// 👇️ convert ms to hours min sec ms
const hoursBetweenDates = msBetweenDates / (60 * 60 * 1000);

data = `
hoursBetweenDates: ${hoursBetweenDates}<br/>
date is within 24 hours: ${
hoursBetweenDates < 24 ? '<span class=green>true</span>' : '<span class=red>false</span>'
}<br/>
date is greater than 48 hours: ${
hoursBetweenDates > 48 ? '<span class=green>true</span>' : '<span class=red>false</span>'
}<br/>
`;
document.getElementById('hours').innerHTML = data;

// unused
function _isOverEighteen(year = 0, month = 0, day = 0) {
var now = parseInt(new Date().toISOString().slice(0, 10).replace(/-/g, ''));
var dob = year * 10000 + month * 100 + day * 1; // Coerces strings to integers

return now - dob > 180000;
}

1
{% include_file lang:javascript 'downloads/code/test.js' %}{% endinclude_file %}
test.jsview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// global variables
const toMatch = new Date('2022-12-01T09:30:20');
let data = `date to match: ${toMatch}<br/>`;
document.getElementById('variables').innerHTML = data;

// method 1 using timestamp

const yourDate = toMatch.getTime(); //Get the timestamp
const today = Date.now();
// 2 days in millisecond
const day = 60 * 60 * 1000 * 24 * 2;
data = `
today: ${today}<br/>
yourDate: ${yourDate}<br/>
2 days in millisecond: ${day}<br/>
today - yourDate: ${today - yourDate}<br/>
is greater than 2 days: ${today - yourDate > day ? '<span class=green>true</span>' : '<span class=red>false</span>'}`;
document.getElementById('timestamp').innerHTML = data;

// method 2 using hours
const now = new Date();

const msBetweenDates = Math.abs(toMatch.getTime() - now.getTime());

// 👇️ convert ms to hours min sec ms
const hoursBetweenDates = msBetweenDates / (60 * 60 * 1000);

data = `
hoursBetweenDates: ${hoursBetweenDates}<br/>
date is within 24 hours: ${
hoursBetweenDates < 24 ? '<span class=green>true</span>' : '<span class=red>false</span>'
}<br/>
date is greater than 48 hours: ${
hoursBetweenDates > 48 ? '<span class=green>true</span>' : '<span class=red>false</span>'
}<br/>
`;
document.getElementById('hours').innerHTML = data;

// unused
function _isOverEighteen(year = 0, month = 0, day = 0) {
var now = parseInt(new Date().toISOString().slice(0, 10).replace(/-/g, ''));
var dob = year * 10000 + month * 100 + day * 1; // Coerces strings to integers

return now - dob > 180000;
}

avatar
Dimas Lanjaka (L3n4r0x)
All in one collection shortcodes for hexo
Follow Me
Announcement
This is my Blog
Recent Post
Info
Article :
0
UV :
PV :
Last Push :