Skip to content

Commit

Permalink
plugins通过id区分markdown-it和posthtml
Browse files Browse the repository at this point in the history
  • Loading branch information
ksky521 committed Jan 25, 2021
1 parent 2984d46 commit 44f4bab
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 47 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[{.travis.yml,ci.yml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
1 change: 0 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"singleQuote": true,
"semi": true,
"printWidth": 120,
"tabWidth": 4,
"bracketSpacing": false,
Expand Down
78 changes: 39 additions & 39 deletions packages/nodeppt-parser/lib/get-parser.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
const posthtml = require('posthtml')
const getMdParser = require('./get-markdown-parser')
const posthtml = require('posthtml');
const getMdParser = require('./get-markdown-parser');
// 内置 posthtml 插件
const buildInPlugins = [
'./tags/slide.js',
'./tags/note.js',
'./tags/header-footer.js',
// attrs放到最后
'./tags/attrs.js'
]
const buildInPosthtmlPlugins = buildInPlugins.map(file => {
return require(file)
})
'./tags/slide.js',
'./tags/note.js',
'./tags/header-footer.js',
// attrs放到最后
'./tags/attrs.js',
];
const buildInPosthtmlPlugins = buildInPlugins.map((file) => {
return require(file);
});

module.exports = plugins => {
const markdownPlugins = []
const posthtmlPlugins = []
module.exports = (plugins) => {
const markdownPlugins = [];
const posthtmlPlugins = [];

plugins.forEach(p => {
if (p && typeof p.apply === 'function') {
if (p.id.indexOf('markdown') === 0) {
markdownPlugins.push(p.apply)
} else if (p.id.indexOf('posthtml') === 0) {
posthtmlPlugins.push(p.apply)
}
}
})
const mdRender = getMdParser(markdownPlugins)
plugins.forEach((p) => {
if (p && typeof p.apply === 'function') {
if (p.id.indexOf('markdown') === 0) {
markdownPlugins.push(p.apply);
} else if (p.id.indexOf('posthtml') === 0) {
posthtmlPlugins.push(p.apply);
}
}
});
const mdRender = getMdParser(markdownPlugins);

return str => {
const slideTag = str.match(/\n<slide\s*(.*)>/mgi) || []
const contents = str.split(/\n<slide.*>/mi)
contents.shift()
return contents
.map((c, i) => {
// 生成 attr
const html = `
return (str) => {
const slideTag = str.match(/\n<slide\s*(.*)>/gim) || [];
const contents = str.split(/\n<slide.*>/im);
contents.shift();
return contents
.map((c, i) => {
// 生成 attr
const html = `
${slideTag[i]}
<div class="wrap" wrap="true">
${mdRender(c)}
</div>
</slide>
`
// 生成 content ast
return posthtml(buildInPosthtmlPlugins.concat(posthtmlPlugins)).process(html, {sync: true}).html
})
.join('\n')
}
}
`;
// 生成 content ast
return posthtml(buildInPosthtmlPlugins.concat(posthtmlPlugins)).process(html, {sync: true}).html;
})
.join('\n');
};
};
16 changes: 10 additions & 6 deletions packages/nodeppt-serve/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ module.exports = class Service {
debug(projectOptions);
// apply plugins.
this.plugins.forEach(({id, apply}) => {
if (id.indexOf('markdown') === 0 || id.indexOf('posthtml') === 0) {
// 如果是以markdown 和posthtml开头的,则不是service插件,而是markdown-it和posthtml插件
return;
}
apply(new PluginAPI(id, this), projectOptions);
});
if (projectOptions.chainWebpack) {
Expand Down Expand Up @@ -106,9 +110,9 @@ module.exports = class Service {
return fn(args, rawArgv);
}
resolvePlugins(inlinePlugins, useBuiltIn) {
const idToPlugin = id => ({
const idToPlugin = (id) => ({
id: id.replace(/^.\//, 'built-in:'),
apply: require(id)
apply: require(id),
});

let plugins;
Expand All @@ -121,7 +125,7 @@ module.exports = class Service {
'./config/css',
'./config/dev',
'./config/prod',
'./config/app'
'./config/app',
].map(idToPlugin);

if (inlinePlugins) {
Expand All @@ -139,7 +143,7 @@ module.exports = class Service {
resolveChainableWebpackConfig() {
const chainableConfig = new Config();
// apply chains
this.webpackChainFns.forEach(fn => fn(chainableConfig));
this.webpackChainFns.forEach((fn) => fn(chainableConfig));
return chainableConfig;
}
resolveWebpackConfig(chainableConfig = this.resolveChainableWebpackConfig()) {
Expand All @@ -150,7 +154,7 @@ module.exports = class Service {
let config = chainableConfig.toConfig();
const original = config;
// apply raw config fns
this.webpackRawConfigFns.forEach(fn => {
this.webpackRawConfigFns.forEach((fn) => {
if (typeof fn === 'function') {
// function with optional return value
const res = fn(config);
Expand Down Expand Up @@ -181,7 +185,7 @@ function cloneRuleNames(to, from) {
from.forEach((r, i) => {
if (to[i]) {
Object.defineProperty(to[i], '__ruleNames', {
value: r.__ruleNames
value: r.__ruleNames,
});
cloneRuleNames(to[i].oneOf, r.oneOf);
}
Expand Down
1 change: 0 additions & 1 deletion packages/nodeppt-serve/config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ module.exports = (api, options) => {
.add('node_modules')
.add(api.resolve('node_modules'))
.add(resolveLocal('node_modules'));

webpackConfig.module
.rule('markdown')
.test(/\.(md|markdown)$/)
Expand Down

0 comments on commit 44f4bab

Please sign in to comment.