Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cannot find installed markdown-it plugin by yarn v3 monorepo #199

Merged
merged 4 commits into from
May 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions lib/renderer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const MarkdownIt = require('markdown-it');
const path = require('path');

dimaslanjaka marked this conversation as resolved.
Show resolved Hide resolved
class Renderer {

/**
* constructor
*
Expand All @@ -17,9 +17,11 @@ class Renderer {
// Temporary backward compatibility
if (typeof markdown === 'string') {
markdown = {
preset: markdown
preset: markdown,
};
hexo.log.warn(`Deprecated config detected. Please use\n\nmarkdown:\n preset: ${markdown.preset}\n\nSee https://github.com/hexojs/hexo-renderer-markdown-it#options`);
hexo.log.warn(
`Deprecated config detected. Please use\n\nmarkdown:\n preset: ${markdown.preset}\n\nSee https://github.com/hexojs/hexo-renderer-markdown-it#options`
);
}

const { preset, render, enable_rules, disable_rules, plugins, anchors, images } = markdown;
Expand All @@ -36,7 +38,19 @@ class Renderer {
if (plugins) {
this.parser = plugins.reduce((parser, pugs) => {
if (pugs instanceof Object && pugs.name) {
return parser.use(require(pugs.name), pugs.options);
const resolved = require.resolve(pugs.name, {
paths: [
// find from root hexo base directory node_modules
path.join(hexo.base_dir, 'node_modules'),
// find from current installed library node_modules
path.join(__dirname, '../node_modules'),
// find from root hexo base directory
hexo.base_dir,
// find from current library directory
path.join(__dirname, '../'),
],
});
return parser.use(require(resolved), pugs.options);
}
return parser.use(require(pugs));
}, this.parser);
Expand All @@ -49,15 +63,15 @@ class Renderer {
if (images) {
this.parser.use(require('./images'), {
images,
hexo: this.hexo
hexo: this.hexo,
});
}
}

render(data, options) {
this.hexo.execFilterSync('markdown-it:renderer', this.parser, { context: this });
return this.parser.render(data.text, {
postPath: data.path
postPath: data.path,
});
}
}
Expand Down