hexojs / hexojs/hexo

Renderer not completely working if not using content parameter for tag plugins

Open
#5,541 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
41.8k
Forks
41
PR merge metrics
No merged PRs in 30d

Description

### Check List

- [x] I have already read [Docs page](https://hexo.io/docs/) & [Troubleshooting page](https://hexo.io/docs/troubleshooting).
- [X] I have already searched existing issues and they are not help to me.
- [ ] I examined error or warning messages and it's difficult to solve.
- [X] I am using the [latest](https://github.com/hexojs/hexo/releases) version of Hexo. (run `hexo version` to check)
- [X] My Node.js is matched [the required version](https://hexo.io/docs/#Required-Node-js-version).

### Expected behavior

The renderer should correctly renders the given text or source content, not just viable when using the `content` parameter.

### Actual behavior

I want to create a notation bubble (something like a dictionary) for the section which is able to be reused by defining a tag plugin, the notation is created in markdown format file. However, when I develop this tag plugin, I found something abnormal behavior from the hexo render.

_**TL;DR: The hexo render only renders correctly when directly using the `content` parameter. It can only render basic markdown, others like tag plugins, code block cannot be correctly rendered.**_

## Retrieving the file content as the markdown content
bubble.js
```js
const fs = require('hexo-fs');

hexo.extend.tag.register('bubble', bubble, { async: true });

const REGEX = /^---\n([\s\S]*?)\n---/;

function bubble(args) {
const notationPath = hexo.source_dir + 'assets/notation/' + args[0] + '.md';
const notation = fs.readFileSync(notationPath).trim();

const properties = getProperty(notation);
const title = properties ? properties.title : 'Unnamed';

const notationContent = hexo.render.renderSync({
text: notation.replace(REGEX, ''),
engine: 'markdown'
})

return `

${title}


${notationContent}


`
}

function getProperty(notation) {
const frontMatterRegex = REGEX;
const match = notation.match(frontMatterRegex);

if (match && match[1]) {
const frontMatter = match[1];
const properties = {};

frontMatter.split('\n').forEach(line => {
const [key, value] = line.split(':').map(item => item.trim());
if (key) {
properties[key] = value;
}
});

return properties;
}

return null;
}
```

here is the final HTML of the notation:
```html


testing paragraph


another paragraph



  1. item1

  2. item2

  3. item3


const express = require('express');

const app = express();
app.get('/', (req, res) => {

});
app.listen(3000);



```

the result preview:
螢幕截圖 2024-09-02 下午12 55 57

as you see, the fenced code block only defines the `

` tag, instead of the hexo's modified one (`figure` > `table`)

## Retrieving content parameter as the markdown content

bubble2.js

```js
hexo.extend.tag.register('bubble2', bubble, { ends: true, async: true });

const REGEX = /^---\n([\s\S]*?)\n---/;

function bubble(args, content) {
const properties = getProperty(content);
const title = properties ? properties.title : 'Unnamed';

const notationContent = hexo.render.renderSync({
text: content.replace(REGEX, ''),
engine: 'markdown'
})

return `

${title}


${notationContent}


`
}

function getProperty(notation) {
const frontMatterRegex = REGEX;
const match = notation.match(frontMatterRegex);

if (match && match[1]) {
const frontMatter = match[1];
const properties = {};

frontMatter.split('\n').forEach(line => {
const [key, value] = line.split(':').map(item => item.trim());
if (key) {
properties[key] = value;
}
});

return properties;
}

return null;
}
```

the final HTML:
```html


testing paragraph


another paragraph



  1. item1

  2. item2

  3. item3


js
1
2
3
4
5
6
const express = require('express');
const app = express();
app.get('/', (req, res) => {

});
app.listen(3000);


```

the result preview:
螢幕截圖 2024-09-02 下午12 56 10

### How to reproduce?

I found this bug when I trying to render markdown content that is not from `content` parameter.

### Is the problem still there under `Safe mode`?

yes

### Your Node.js & npm version

```text
node: v18.12.1
npm: 9.8.1
```

### Your Hexo and Plugin version

```text
hexo-site@0.0.0 /Users/bluewhaleyt/PersonalProjects/Blog
├── @types/node@22.5.0
├── hexo-asset-image@1.0.0
├── hexo-browsersync@0.3.0
├── hexo-generator-archive@2.0.0
├── hexo-generator-category@2.0.0
├── hexo-generator-index@4.0.0
├── hexo-generator-search@2.4.3
├── hexo-generator-tag@2.0.0
├── hexo-pangu@0.2.2
├── hexo-renderer-ejs@2.0.0
├── hexo-renderer-marked@6.3.0
├── hexo-renderer-pug@3.0.0
├── hexo-renderer-stylus@3.0.1
├── hexo-server@3.0.0
├── hexo-theme-landscape@1.0.0
├── hexo-theme-solitude@2.0.11
├── hexo-wordcount@6.0.1
└── hexo@7.3.0
```

### Your `package.json`

```json
{
"name": "hexo-site",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "hexo generate",
"clean": "hexo clean",
"deploy": "hexo deploy",
"server": "hexo server"
},
"hexo": {
"version": "7.3.0"
},
"dependencies": {
"hexo": "^7.3.0",
"hexo-asset-image": "^1.0.0",
"hexo-browsersync": "^0.3.0",
"hexo-generator-archive": "^2.0.0",
"hexo-generator-category": "^2.0.0",
"hexo-generator-index": "^4.0.0",
"hexo-generator-search": "^2.4.3",
"hexo-generator-tag": "^2.0.0",
"hexo-pangu": "^0.2.2",
"hexo-renderer-ejs": "^2.0.0",
"hexo-renderer-marked": "^6.3.0",
"hexo-renderer-pug": "^3.0.0",
"hexo-renderer-stylus": "^3.0.1",
"hexo-server": "^3.0.0",
"hexo-theme-landscape": "^1.0.0",
"hexo-theme-solitude": "^2.0.10",
"hexo-wordcount": "^6.0.1"
},
"devDependencies": {
"@types/node": "^22.5.0"
}
}
```

### Your site's `_config.yml` (Optional)

_No response_

### Others

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.