feat(v2): truncate marker as blog option (#1706)

* feat(v2): allowed more as truncate marker

* feat(v2): blog support truncateMarker option

* feat(v2): blog support truncateMarker option
This commit is contained in:
陈杨文 2019-07-27 17:07:26 +08:00 committed by Endi
parent e38373ac9b
commit d17a1ea9e3
3 changed files with 16 additions and 6 deletions

View file

@ -2,6 +2,7 @@
## Unreleased ## Unreleased
- Add `truncateMarker` option to blog plugin, support string or regex.
- Webpack `optimization.removeAvailableModules` is now disabled for performance gain. See https://github.com/webpack/webpack/releases/tag/v4.38.0 for more context. - Webpack `optimization.removeAvailableModules` is now disabled for performance gain. See https://github.com/webpack/webpack/releases/tag/v4.38.0 for more context.
## 2.0.0-alpha.24 ## 2.0.0-alpha.24

View file

@ -31,6 +31,7 @@ const DEFAULT_OPTIONS = {
blogTagsPostsComponent: '@theme/BlogTagsPostsPage', blogTagsPostsComponent: '@theme/BlogTagsPostsPage',
remarkPlugins: [], remarkPlugins: [],
rehypePlugins: [], rehypePlugins: [],
truncateMarker: /<!--\s*(truncate)\s*-->/, // string or regex
}; };
module.exports = function(context, opts) { module.exports = function(context, opts) {
@ -340,7 +341,7 @@ module.exports = function(context, opts) {
}, },
configureWebpack(config, isServer, {getBabelLoader, getCacheLoader}) { configureWebpack(config, isServer, {getBabelLoader, getCacheLoader}) {
const {rehypePlugins, remarkPlugins} = options; const {rehypePlugins, remarkPlugins, truncateMarker} = options;
return { return {
module: { module: {
rules: [ rules: [
@ -359,6 +360,9 @@ module.exports = function(context, opts) {
}, },
{ {
loader: path.resolve(__dirname, './markdownLoader.js'), loader: path.resolve(__dirname, './markdownLoader.js'),
options: {
truncateMarker,
},
}, },
].filter(Boolean), ].filter(Boolean),
}, },

View file

@ -5,20 +5,25 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
const {parseQuery} = require('loader-utils'); const {parseQuery, getOptions} = require('loader-utils');
const TRUNCATE_MARKER = /<!--\s*truncate\s*-->/;
module.exports = async function(fileString) { module.exports = async function(fileString) {
const callback = this.async(); const callback = this.async();
const {truncateMarker} = getOptions(this);
let finalContent = fileString; let finalContent = fileString;
// Truncate content if requested (e.g: file.md?truncated=true) // Truncate content if requested (e.g: file.md?truncated=true)
const {truncated} = this.resourceQuery && parseQuery(this.resourceQuery); const {truncated} = this.resourceQuery && parseQuery(this.resourceQuery);
if (truncated && TRUNCATE_MARKER.test(fileString)) { if (
truncated &&
(typeof truncateMarker === 'string'
? fileString.includes(truncateMarker)
: truncateMarker.test(fileString))
) {
// eslint-disable-next-line // eslint-disable-next-line
finalContent = fileString.split(TRUNCATE_MARKER)[0]; finalContent = fileString.split(truncateMarker)[0];
} }
return callback(null, finalContent); return callback(null, finalContent);
}; };