hexo-breadcrumb
Advanced tools
Comparing version 1.0.2 to 1.0.3
103
index.js
@@ -1,31 +0,86 @@ | ||
const config = hexo.config | ||
const breadcrumb = hexo.config.breadcrumb | ||
const { config } = hexo; | ||
const { breadcrumb } = hexo.config; | ||
hexo.extend.filter.register('before_post_render', function(data){ | ||
// return data if breadcumb doesn't exist and data's layout isn't post | ||
if ((!breadcrumb || !breadcrumb.hasOwnProperty('display')) || data.layout !== 'post') return data | ||
/** | ||
* Convert links to HTML | ||
* | ||
* @param {Array<{ url: string, title: string}>} links - Array of link objects | ||
* @return {string} links_ol - HTML markup for the links | ||
*/ | ||
function toHTML(links) { | ||
const links_li = links | ||
.map(function (link) { | ||
return `<li><a href="${link.url}"><span>${link.title}</span></a></li>`; | ||
}) | ||
.join(""); | ||
const links = [{ | ||
title: breadcrumb.homepage && breadcrumb.homepage.title ? breadcrumb.homepage.title : config.title ? config.title : 'Homepage', | ||
url: breadcrumb.homepage && breadcrumb.homepage.url ? breadcrumb.homepage.url : config.url ? config.url : '/', // should be absolute address | ||
position: breadcrumb.homepage && breadcrumb.homepage.position ? breadcrumb.homepage.position : 1 | ||
}, { | ||
title: data.title !== '' ? data.title : data.slug, | ||
url: data.permalink, // should be absolute address | ||
position: 2 | ||
}] | ||
return `<ul id="hexo-breadcrumb">${links_li}</ul>`; | ||
} | ||
data.breadcrumb = { | ||
html: nav_generator(links), | ||
links: links | ||
/** | ||
* Get ordered links by matrix | ||
* | ||
* @param {string} layout | ||
* @param {Array<Record<number, { layout: string }>>} matrix | ||
* @param {Array<{ url: string, title: string}>} links - Array of link objects | ||
* @return {Array<string>} | ||
*/ | ||
function getOrderedLinksByMatrix(layout, matrix, links) { | ||
const detectedLayout = list.find(function (item) { | ||
return item.layout === layout; | ||
}); | ||
return detectedLayout.format | ||
.map(function (key, index) { | ||
return links[key]; | ||
}) | ||
.flat(); | ||
} | ||
function setupBreadcrumb(data) { | ||
if (!breadcrumb && !breadcrumb.homepage) { | ||
return data; | ||
} | ||
}) | ||
function nav_generator(list) { | ||
let result = '' | ||
list.forEach(link => { | ||
result += `<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" href="${link.url}"><span itemprop="name">${link.title}</span></a><meta itemprop="position" content="${link.position}" /></li>` | ||
}) | ||
const { layout } = data; | ||
const { homepage, matrix } = breadcrumb; | ||
return `<ol class="breadcrumb" ${breadcrumb.display ? '' : `style="display: none"`} itemscope itemtype="http://schema.org/BreadcrumbList">${result}</ol>` | ||
const homeLink = { | ||
title: homepage?.title || config.title, | ||
url: config.url, | ||
}; | ||
const categoryLinks = data.categories.data.map(function (category, index) { | ||
return { | ||
title: category.name, | ||
url: category.permalink, | ||
}; | ||
}); | ||
const titleLink = { | ||
title: data?.title || data.slug, | ||
url: data.permalink, | ||
}; | ||
const unorderedLinks = { | ||
home: homeLink, | ||
category: categoryLinks, | ||
title: titleLink, | ||
}; | ||
const links = getOrderedLinksByMatrix(layout, matrix, unorderedLinks); | ||
console.log({ links }); | ||
return toHTML(links); | ||
} | ||
// Register before_post_render filter | ||
// Ref: https://hexo.io/api/filter#before-post-render | ||
hexo.extend.filter.register("before_post_render", function (data) { | ||
if (data.layout !== "post" && data.layout !== "page") { | ||
return data; | ||
} | ||
data.breadcrumb = setupBreadcrumb(data); | ||
}); |
{ | ||
"name": "hexo-breadcrumb", | ||
"version": "1.0.2", | ||
"description": "An useful tool for generate breadcrumb navigation with JSON-LD for hexo posts ", | ||
"version": "1.0.3", | ||
"description": "Generate breadcrumb for a Hexo page", | ||
"main": "index.js", | ||
@@ -14,6 +14,5 @@ "repository": { | ||
"hexo", | ||
"json-ld", | ||
"seo" | ||
], | ||
"author": "Masoud Ghorbani <msud.ghorbani@gmail.com>", | ||
"author": "Masoud Ghorbani <masoudghorbani@pm.me>", | ||
"license": "MIT", | ||
@@ -26,3 +25,7 @@ "bugs": { | ||
"node": ">=7.5.0" | ||
}, | ||
"devDependencies": { | ||
"@types/hexo": "^3.8.8", | ||
"prettier": "2.8.8" | ||
} | ||
} |
# hexo-breadcrumb | ||
An useful tool for generate breadcrumb navigation with microdata markup for hexo posts | ||
![MIT License](https://img.shields.io/npm/l/hexo-breadcrumb?style=social) | ||
![NPM Version](https://img.shields.io/npm/v/hexo-breadcrumb?style=social) | ||
Generate breadcrumb for a Hexo post/page. | ||
# Install | ||
**NPM** | ||
```bash | ||
@@ -9,17 +16,35 @@ $ npm install hexo-breadcrumb --save-dev | ||
**Yarn** | ||
```bash | ||
$ yarn add hexo-breadcrumb --dev | ||
``` | ||
# Usage | ||
#### Config | ||
| Property | Type | Value | | ||
|----------|---------|----------------------------------------------------| | ||
| display | boolean | hidden breadcrumb html list or not by css property | | ||
| homepage | record | Overwrite homepage link context | | ||
First add configuration in **`_config.yml`** from your hexo project. | ||
```yaml | ||
breadcrumb: | ||
display: true | ||
homepage: | ||
# Override the default homepage title in breadcrumb. | ||
title: Home | ||
matrix: | ||
- layout: post | ||
format: | ||
- home | ||
- category | ||
- title | ||
- layout: page | ||
format: | ||
- home | ||
- title | ||
``` | ||
Second, print out the breadcrumb in post layout | ||
Links are ordered based on the determined `matrix` array per layout in `_config.yml` file. | ||
After configuration, Add the following snippet to your post/page layout file to render breadcrumb in layouts. | ||
**post**: | ||
```ejs | ||
@@ -29,14 +54,10 @@ <%- post.breadcrumb.html %> | ||
# Tips | ||
* To overwrite homepage link do same as below: | ||
```yaml | ||
breadcrumb: | ||
display: true | ||
homepage: | ||
title: Home | ||
url: http://example.com # should be absolute address | ||
``` | ||
* To hide breadcrumb html list in css way just set `display` property to `false` | ||
**page**: | ||
```ejs | ||
<%- page.breadcrumb.html %> | ||
``` | ||
# License | ||
[MIT](https://msudgh.mit-license.org/) | ||
The project is licensed under the MIT [License](LICENSE). |
Sorry, the diff of this file is not supported yet
5431
6
71
62
2