Skip to content

Commit

Permalink
🫡添加排除页面配置项(@xyhcode),完善配置项置空时的判断和处理
Browse files Browse the repository at this point in the history
  • Loading branch information
willow-god committed Aug 15, 2024
1 parent 4e68c70 commit 08918c8
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 80 deletions.
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## hexo-safego

[NPM 发布地址](https://www.npmjs.com/package/hexo-safego) | [详细说明文档](https://blog.qyliu.top/posts/1dfd1f41/)
[NPM 发布地址](https://www.npmjs.com/package/hexo-safego) | [详细说明文档](https://blog.liushen.fun/posts/1dfd1f41/)

`hexo-safego` 是一个改进版的 Hexo 插件,用于处理外部链接以增强博客的安全性。该插件基于 [`hexo-external-link`](https://github.com/hvnobug/hexo-external-link) 二次开发,但其实现方式不同:原版插件通过注入 JS ,在打开网站时才处理外部链接,而 `hexo-safego` 则在生成静态页面时直接替换外部链接,并且提供了很多人性化的配置。

Expand Down Expand Up @@ -46,12 +46,12 @@ hexo_safego:
apply_containers: # 容器 ID 列表,如果为空则匹配整个 body
- '#article-container'
domain_whitelist: # 域名白名单列表,包含白名单中的域名的链接将被忽略
- 'qyliu.top'
exclude_pages:
- 'fcircle/index.html'
- 'example.com'
exclude_pages: # 排除路径列表,会排除这些页面,可以配合生效页面路径配合使用
- '/safego_exclude_page/'
apply_pages: # 生效页面路径列表,只有在这些页面上才会对链接进行处理
- '/posts/'
avatar: /info/avatar.ico # 头像图片链接
avatar: https://fastly.jsdelivr.net/gh/willow-god/hexo-safego@latest/lib/avatar.png # 头像图片链接
title: "清羽飞扬" # 标题
subtitle: "安全中心" # 副标题
darkmode: false # 是否启用夜间模式
Expand All @@ -61,10 +61,10 @@ hexo_safego:
### 必要修改说明
- **`domain_whitelist`**: 该参数为您的站点根域名。此配置使用字符串匹配,如果外链中包含您填写的字符串,则会跳过该外链不进行处理。可以设置多个域名。
- **`apply_containers`**: 该参数为您希望处理的容器选择器。如果需要类名选择器,可以使用 `.类名` 进行筛选。如果希望匹配整个网站,请填写 `body` 或者删除该配置项。由于我使用的是 Butterfly 主题,因此填写了 Butterfly 主题中文章部分的选择器 `#article-container`。
- **`apply_pages`**: 该参数指定插件生效的页面路径。如果您只希望在文章页面中使用插件,可以像示例配置中一样设置路径。如果希望插件在整个网站生效,可以设置为 `'/'`。
- **`avatar、title、subtitle`**: 请设置为您的个人信息,以提高头像显示速度。默认头像为 `jsdelivr` 图片,国内访问可能会影响加载速度,建议使用本地图片链接
- **注意**: 以上配置项中未设置空值判断,请不要留空。如果您不需要某个配置项并希望使用默认值,请直接删除对应配置项!
- **`apply_containers`**: 该参数为您希望处理的容器选择器。如果需要类名选择器,可以使用 `.类名` 进行筛选。如果希望匹配整个网站,请填写 `body` 或者置空 or 删除该配置项。由于我使用的是 Butterfly 主题,因此填写了 Butterfly 主题中文章部分的选择器 `#article-container` , 请按需设置
- **`apply_pages`**: 该参数指定插件生效的页面路径。如果您只希望在文章页面中使用插件,可以像示例配置中一样设置路径为 `'/post/'`。如果希望插件在整个网站生效,可以设置为 `'/'` 或者置空 or 删除该项配置
- **`avatar、title、subtitle`**: 请设置为您的个人信息,以提高头像显示速度。默认头像为 `jsdelivr` 图片,国内访问可能会影响加载速度。支持使用本地相对路径图片或网络图片链接
- **注意**: 以上配置项中部分可能空值判断不准确,仅作为防止出现异常的处理,如果使用请尽量不要留空。如果您不需要某个配置项并希望使用默认值,请直接删除对应配置项或者设置为对应默认值。

通过以上配置,您可以更好地自定义 `hexo-safego` 插件的行为和外观,确保外部链接处理更加安全和符合您的需求。

Expand All @@ -83,7 +83,7 @@ hexo_safego:
#### `enable_target_blank`
- **类型**:`Boolean`
- **默认值**:`true`
- **描述**:是否在跳转链接中添加 `target="_blank"` 属性。
- **描述**:是否在跳转链接中添加 `target="_blank"` 属性,建议添加,可以在新页面打开

#### `url_param_name`
- **类型**:`String`
Expand All @@ -102,23 +102,28 @@ hexo_safego:

#### `apply_containers`
- **类型**:`Array`
- **默认值**:`['#article-container']`
- **默认值**:`['body']`
- **描述**:指定要处理的容器 ID 列表。如果为空,则匹配整个 `body`。

#### `domain_whitelist`
- **类型**:`Array`
- **默认值**:`[]`
- **默认值**:`["example.com"]`
- **描述**:域名白名单列表,包含白名单中的域名的链接将被忽略。

#### `apply_pages`
- **类型**:`Array`
- **默认值**:`['/posts/']`
- **默认值**:`['/']`
- **描述**:生效页面路径列表,只有在这些页面上才会对链接进行处理。如果希望插件对整个网站生效,可以设置为 `['/']`。

#### `exclude_pages`
- **类型**:`Array`
- **默认值**:`[]`
- **描述**:生效页面路径列表,只有在这些页面上才会对链接进行处理,建议和apply_page搭配使用,可以更加准确的限制路径,如果不配置或置为空不会影响。

#### `avatar`
- **类型**:`String`
- **默认值**:`https://fastly.jsdelivr.net/gh/willow-god/hexo-safego@latest/lib/avatar.png`
- **描述**:跳转页面的头像图片链接。
- **描述**:跳转页面的头像图片链接,支持相对路径本地图片和绝对路径网络图片

#### `title`
- **类型**:`String`
Expand All @@ -142,4 +147,4 @@ hexo_safego:

### 问题反馈

如果有任何问题,请提 issue 或者联系作者邮箱 01@liushen.fun,或者在网站中进行提问:https://blog.qyliu.top
如果有任何问题,请提 issue 或者联系作者邮箱 01@liushen.fun,或者在网站中进行提问:https://blog.liushen.fun
97 changes: 45 additions & 52 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

## hexo-safego

[NPM Page](https://www.npmjs.com/package/hexo-safego) | [Detailed Documentation](https://blog.qyliu.top/posts/1dfd1f41/)
[NPM Release Address](https://www.npmjs.com/package/hexo-safego) | [Detailed Documentation](https://blog.liushen.fun/posts/1dfd1f41/)

`hexo-safego` is an improved Hexo plugin designed to handle external links to enhance blog security. This plugin is a secondary development based on [`hexo-external-link`](https://github.com/hvnobug/hexo-external-link), but its implementation is different: the original plugin injects JS to process external links when opening the website, while `hexo-safego` replaces external links directly when generating static pages and provides many user-friendly configurations.
`hexo-safego` is an improved version of the Hexo plugin designed to handle external links to enhance the security of your blog. This plugin is a secondary development based on [`hexo-external-link`](https://github.com/hvnobug/hexo-external-link), but with a different implementation: the original plugin processes external links when opening the site through injected JS, while `hexo-safego` directly replaces external links when generating static pages and provides many user-friendly configurations.

### Main Features
### Key Features

- **External Link Redirection**: Replace external links with a custom redirection page to increase security.
- **Flexible Configuration**: Supports multiple container IDs, whitelisted domains, and applicable page paths.
- **Base64 Encoding**: Optional Base64 encoding for links.
- **Debug Mode**: Outputs detailed information in debug mode, making development and debugging easier.
- **Custom Page**: Supports setting title, subtitle, avatar, dark mode, and more.
- **External Link Redirection**: Replace external links with custom redirection pages to increase security.
- **Flexible Configuration**: Supports multiple container IDs, domain whitelists, and active page paths configurations.
- **Base64 Encoding**: Optional Base64 encoding feature.
- **Debug Mode**: Detailed output in debug mode for development and debugging.
- **Custom Pages**: Allows setting up titles, subtitles, avatars, dark mode, and more.

### Installation

Before using this plugin, you need to install `cheerio`. Hexo usually has this plugin, and you can check it in `node_modules`. If not, run:
Before using this plugin, you need to install `cheerio`. Hexo generally includes this plugin, which you can check in `node_modules`. If not present, run:

```bash
npm install cheerio --save
Expand All @@ -30,128 +30,121 @@ npm install hexo-safego --save

### Configuration

Add or update the following configuration in Hexo's `_config.yml` file:
Add or update the following configuration in your Hexo configuration file `_config.yml`:

```yaml
# hexo-safego security redirection plugin
# see https://blog.qyliu.top/posts/1dfd1f41/
hexo_safego:
enable: true # Enable hexo-safego plugin
enable_base64_encode: true # Enable Base64 encoding for links
enable: true # Enable the hexo-safego plugin
enable_base64_encode: true # Enable Base64 encoding of links
enable_target_blank: true # Add target="_blank" to redirection links
url_param_name: 'u' # URL parameter name for generating redirection links
html_file_name: 'go.html' # Redirection page filename
html_file_name: 'go.html' # Redirection page file name
ignore_attrs: # List of link attributes to ignore
- 'data-fancybox'
apply_containers: # List of container IDs to apply the plugin to, matches entire body if empty
apply_containers: # Container ID list, if empty, applies to the entire body
- '#article-container'
domain_whitelist: # List of whitelisted domains, links containing these domains will be ignored
- 'qyliu.top'
apply_pages: # List of page paths to apply the plugin to, only links on these pages will be processed
domain_whitelist: # Domain whitelist list; links containing whitelisted domains will be ignored
- 'example.com'
exclude_pages: # Exclude paths list; these pages will be excluded, can be used with apply_pages
- '/safego_exclude_page/'
apply_pages: # Effective page path list; links will only be processed on these pages
- '/posts/'
avatar: /info/avatar.ico # Avatar image link
avatar: https://fastly.jsdelivr.net/gh/willow-god/hexo-safego@latest/lib/avatar.png # Avatar image link
title: "Qingyu Feiyang" # Title
subtitle: "Security Center" # Subtitle
darkmode: false # Enable dark mode
debug: false # Enable debug mode, outputs detailed debug information
debug: false # Enable debug mode, detailed debug information will be output
```
### Required Modifications
### Important Modifications
- **`domain_whitelist`**: This parameter is your site's root domain. This part uses string matching; if the external link contains the string you filled in, the external link will be skipped. You can set multiple domains.
- **`apply_containers`**: This parameter is the container selector you need. If you need a class selector, you can use `.classname` to filter. If you need to match the entire site, fill in `body` or leave it empty. Here, since I am using the Butterfly theme, I filled in the Butterfly article part selector `#article-container`.
- **`apply_pages`**: This parameter specifies the applicable pages. If you only want to use it on article pages, you can configure it as I did.
- **`avatartitlesubtitle`**: Please set these to your information to speed up avatar loading, as the default avatar is a `jsdelivr` image, which may affect loading speed in China.
- **Note**: The above configuration does not include null checks, so do not leave them empty. If you don't need a configuration and want to use the default, delete the corresponding configuration!
- **`domain_whitelist`**: This parameter is your site's root domain. This configuration uses string matching; if the external link contains the string you provide, it will be skipped. You can set multiple domains.
- **`apply_containers`**: This parameter is the container selector you want to process. If you need class selectors, you can use `.classname` to filter. To match the entire site, you can enter `body` or leave this configuration empty or delete it. Since I use the Butterfly theme, I have set the selector for the article section of the Butterfly theme `#article-container`. Please configure it as needed.
- **`apply_pages`**: This parameter specifies the page paths where the plugin is effective. If you only want the plugin to be used on article pages, you can set the path to `'/post/'` as shown in the example configuration. If you want the plugin to be effective across the entire site, you can set it to `'/'` or leave it empty or delete this item.
- **`avatar, title, subtitle`**: Please set these to your personal information to improve avatar display speed. The default avatar is an image from `jsdelivr`, which might affect loading speed in mainland China. It supports both local relative path images and absolute path network images.
- **Note**: Some of the above configuration items may not have accurate empty value checks, and are only used to prevent exceptions. If using, try not to leave them empty. If you don't need a specific configuration item and want to use the default value, please directly delete the corresponding configuration item or set it to the default value.

With the above configuration, you can better customize the behavior and appearance of the `hexo-safego` plugin to ensure safer and more compliant external link processing.
With the above configuration, you can better customize the behavior and appearance of the `hexo-safego` plugin to ensure external link handling is more secure and meets your needs.

### Configuration Parameters

#### `enable`

- **Type**: `Boolean`
- **Default**: `false`
- **Description**: Whether to enable the `hexo-safego` plugin.

#### `enable_base64_encode`

- **Type**: `Boolean`
- **Default**: `true`
- **Description**: Whether to Base64 encode the redirection links.

#### `enable_target_blank`

- **Type**: `Boolean`
- **Default**: `true`
- **Description**: Whether to add `target="_blank"` to the redirection links.
- **Description**: Whether to add `target="_blank"` to the redirection links. It is recommended to add it so the link opens in a new page.

#### `url_param_name`

- **Type**: `String`
- **Default**: `u`
- **Description**: The URL parameter name for the redirection page.

#### `html_file_name`

- **Type**: `String`
- **Default**: `go.html`
- **Description**: The filename of the generated redirection page.
- **Description**: The file name of the generated redirection page.

#### `ignore_attrs`

- **Type**: `Array`
- **Default**: `['data-fancybox']`
- **Description**: List of link attributes to ignore. Links containing these attributes will not be processed.

#### `apply_containers`

- **Type**: `Array`
- **Default**: `['#article-container']`
- **Description**: List of container IDs to apply the plugin to. If empty, the entire `body` will be matched.
- **Default**: `['body']`
- **Description**: List of container IDs to process. If empty, it will match the entire `body`.

#### `domain_whitelist`

- **Type**: `Array`
- **Default**: `[]`
- **Description**: List of whitelisted domains. Links containing these domains will be ignored.
- **Default**: `["example.com"]`
- **Description**: Domain whitelist list; links containing whitelisted domains will be ignored.

#### `apply_pages`
- **Type**: `Array`
- **Default**: `['/']`
- **Description**: List of page paths where the plugin is effective. Links will only be processed on these pages. To make the plugin effective across the entire site, set this to `['/']`.

#### `exclude_pages`
- **Type**: `Array`
- **Default**: `['/posts/']`
- **Description**: List of page paths to apply the plugin to. Only links on these pages will be processed. To apply the plugin to the entire site, set it to `['/']`.
- **Default**: `[]`
- **Description**: List of page paths to exclude. These pages will be excluded from processing. It is recommended to use this with `apply_pages` to more accurately limit paths. If not configured or set to empty, it will not affect.

#### `avatar`

- **Type**: `String`
- **Default**: `https://fastly.jsdelivr.net/gh/willow-god/hexo-safego@latest/lib/avatar.png`
- **Description**: The avatar image link for the redirection page.
- **Description**: Avatar image link for the redirection page. Supports both relative local path images and absolute path network images.

#### `title`

- **Type**: `String`
- **Default**: `Site Name`
- **Description**: The title of the redirection page.
- **Description**: Title for the redirection page.

#### `subtitle`

- **Type**: `String`
- **Default**: `Site Subtitle`
- **Description**: The subtitle of the redirection page.
- **Description**: Subtitle for the redirection page.

#### `darkmode`

- **Type**: `Boolean`
- **Default**: `false`
- **Description**: Whether to enable dark mode.

#### `debug`

- **Type**: `Boolean`
- **Default**: `false`
- **Description**: Whether to enable debug mode. Outputs detailed debug information when enabled.
- **Description**: Whether to enable debug mode. Detailed debug information will be output when enabled.

### Issue Reporting

If you have any issues, please open an issue or contact the author via email at 01@liushen.fun, or ask questions on the website: https://blog.qyliu.top
If you encounter any issues, please file an issue or contact the author via email at 01@liushen.fun, or ask on the website: https://blog.liushen.fun
Loading

0 comments on commit 08918c8

Please sign in to comment.