Files
Fechin 1cc1a5d37f feat: Enhance page SEO and language support
- Add language attributes in layout files
- Update the content-language meta tag in header files
- Introduce a custom clean_url helper to clean up URLs
- Update SEO scripts to ensure preview images display correctly
2025-06-23 01:14:33 -07:00

71 lines
2.2 KiB
JavaScript

const fs = require('fs');
const path = require('path');
hexo.extend.helper.register('page_title', function () {
let title = '';
if (this.is_home()) {
title = `${this.config.title} - ${this.config.subtitle}`;
} else if (this.page.layout === 'note') {
title = `${this.page.title} - ${this.config.title}`;
} else if (this.is_page()) {
title = `${this.page.title} - ${this.config.title}`;
} else if (this.is_post()) {
title = this.page.title;
const hasCommand = this.page.categories.findOne({ name: 'Linux Command' });
if (hasCommand) {
title += ' Command';
}
title = `${title} Cheat Sheet & Quick Reference`;
} else if (this.page._isNotePagination) {
title = `Page ${this.page.current} of Notes - ${this.config.title}`;
}
return title;
});
hexo.extend.helper.register('page_description', function () {
let description;
if (this.is_home()) {
description = this.config.description;
} else if (this.page && this.page.intro && this.page.intro.trim()) {
description = this.strip_html(this.markdown(this.page.intro)).substring(0, 110);
} else if (this.page && this.page.content) {
description = this.strip_html(this.page.content).substring(0, 130);
} else {
description = this.config.description || '';
}
description = description.replace(/[\r\n\s\s+]+/g, ' ');
return description;
});
hexo.extend.helper.register('page_keywords', function () {
let keywords = this.config.keywords;
if (this.page.tags && this.page.tags.each) {
this.page.tags.each((tag) => {
keywords += `,${tag.name}`;
});
}
return keywords;
});
hexo.extend.helper.register('page_image', function () {
const homePng = 'preview.png';
let postPng = 'preview.png';
if (this.is_post()) {
postPng = `${this.page.slug}-preview.png`;
}
const version = (Math.random() + 1).toString(36).substring(7);
let imageURL;
// Check file exists
const postPath = path.resolve('./', 'source/assets/image/', postPng);
if (fs.existsSync(postPath)) {
// Generate image url
imageURL = `${this.config.url}/assets/image/${postPng}?v=${version}`;
} else {
// console.warn(`Warning: ${postPath} not Found!`);
imageURL = `${this.config.url}/assets/image/${homePng}?v=${version}`;
}
return imageURL;
});