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: enable live reloading title of doc #1507

Merged
merged 7 commits into from
May 27, 2019
Merged
31 changes: 26 additions & 5 deletions packages/docusaurus-1.x/lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ function execute(port, host) {
MetadataBlog = require(join('..', 'core', 'MetadataBlog.js'));
}

function reloadTranslations() {
removeModuleAndChildrenFromCache('./translation.js');
}

function requestFile(url, res, notFoundCallback) {
request.get(url, (error, response, body) => {
if (!error) {
Expand Down Expand Up @@ -116,18 +120,35 @@ function execute(port, host) {

app.get(routing.docs(siteConfig), (req, res, next) => {
const url = decodeURI(req.path.toString().replace(siteConfig.baseUrl, ''));
const metadata =
Metadata[
Object.keys(Metadata).find(id => Metadata[id].permalink === url)
];
const metakey = Object.keys(Metadata).find(
id => Metadata[id].permalink === url,
);

let metadata = Metadata[metakey];

const file = docs.getFile(metadata);
if (!file) {
next();
return;
}
const extractedMetadata = metadataUtils.extractMetadata(file);
const rawContent = extractedMetadata.rawContent;
const rawMetadata = extractedMetadata.metadata;

// if any of the followings is changed, reload the metadata
const reloadTriggers = ['sidebar_label', 'hide_title', 'title'];
for (let i = 0; i < reloadTriggers.length; i++) {
const key = reloadTriggers[i];
if (metadata[key] !== rawMetadata[key]) {
reloadMetadata();
extractTranslations();
reloadTranslations();
metadata = Metadata[metakey];
break;
}
}

reloadSiteConfig();
const rawContent = metadataUtils.extractMetadata(file).rawContent;
removeModuleAndChildrenFromCache('../core/DocsLayout.js');
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig);
res.send(docs.getMarkup(rawContent, mdToHtml, metadata, siteConfig));
Expand Down