docusaurus/lib/core/utils.js
Endilie Yacop Sucipto d18b09954b Fix: transformation of url to cleanUrl / pretty url (#923)
* fix: pretty url should only remove html extension

* cleanUrl of '/index.html' should be '/'
2018-08-27 21:09:50 -07:00

36 lines
865 B
JavaScript

/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const TRUNCATE_MARKER = /<!--\s*truncate\s*-->/;
function blogPostHasTruncateMarker(content) {
return TRUNCATE_MARKER.test(content);
}
function extractBlogPostBeforeTruncate(content) {
return content.split(TRUNCATE_MARKER)[0];
}
function removeExtension(pathStr) {
return pathStr.replace(/\.[^/.]+$/, '');
}
function getPath(pathStr, cleanUrl = false) {
if (!pathStr || !cleanUrl || !pathStr.endsWith('.html')) {
return pathStr;
}
return pathStr.endsWith('/index.html')
? pathStr.replace(/index\.html$/, '')
: removeExtension(pathStr);
}
module.exports = {
blogPostHasTruncateMarker,
extractBlogPostBeforeTruncate,
getPath,
removeExtension,
};