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 (v1): fix broken relative links related to --skip-next-release build option #1816

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 54 additions & 2 deletions packages/docusaurus-1.x/lib/server/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,60 @@ function mdToHtmlify(oldContent, mdToHtml, metadata, siteConfig) {
const docsSource = metadata.version
? metadata.source.replace(/version-.*?\//, '')
: metadata.source;
let htmlLink =
mdToHtml[resolve(docsSource, mdMatch[1])] || mdToHtml[mdMatch[1]];

let htmlLink;

if (readMetadata.shouldGenerateNextReleaseDocs()) {
htmlLink =
mdToHtml[resolve(docsSource, mdMatch[1])] || mdToHtml[mdMatch[1]];
} else {
/**
* When the very first version is created, all source/markdown files from next release docs
* are copied to the directory corresponding to that version. For subsequent versiones being
* created, source/markdown files are copied to the corresponding version directory, if and
* only if that file had changed in the next release docs. This is how the source files are
* maintained when versions are created.
*
* During build time those files that were not copied to the versioned docs source, are copied
* to the build output.
*
* When `--skip-next-release` build option is used, we need to verify whether the target file path
* exists with the "versioned docs" or at the next release docs. If it does, that file will be
* available in the build output and the link will be valid. So, update the link from `.md` to
* `.html` accordingly.
*/
const originalFilePath = metadata.original_id.match('.+/')
? metadata.original_id.match('.+/')[0]
: '';

const targetFileAtVersionedDoc =
'versioned_docs/version-' +
metadata.version +
'/' +
resolve(docsSource, mdMatch[1]);

const targetFileAtRoot =
'../' + siteConfig.docsUrl + '/' + originalFilePath + mdMatch[1];

if (
fs.existsSync(targetFileAtVersionedDoc) ||
fs.existsSync(targetFileAtRoot)
) {
if (siteConfig.cleanUrl) {
htmlLink = docsSource.endsWith('index.md')
? mdMatch[1].replace('.md', '.html')
: '../' + mdMatch[1].replace('.md', '.html');
} else {
htmlLink = mdMatch[1].replace('.md', '.html');
}
}
}

/**
* If there is a valid target link, sanitize the URL according to various configuration
* options such as: `clearnUrl`, versions, language, etc. Otherwise, store the target so
* that it can be reported as a broken link.
*/
if (htmlLink) {
htmlLink = getPath(htmlLink, siteConfig.cleanUrl);
htmlLink = htmlLink.replace('/en/', `/${metadata.language}/`);
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-1.x/lib/server/metadataUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function mdToHtml(Metadata, siteConfig) {
const result = {};
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
if (metadata.language !== 'en' || metadata.original_id) {
if (metadata.language !== 'en') {
return;
}
let htmlLink = baseUrl + metadata.permalink.replace('/next/', '/');
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-1.x/lib/server/readMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,5 @@ module.exports = {
processMetadata,
generateMetadataDocs,
generateMetadataBlog,
shouldGenerateNextReleaseDocs,
};