-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from mtmacdonald/refactor-ts
Continue refactoring code to TypeScript + splitting into modules
- Loading branch information
Showing
8 changed files
with
220 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<!DOCTYPE html><html><head> | ||
<title>DocGen - a documentation tool</title> | ||
<meta http-equiv="REFRESH" content="0;url=docs/index.html"> | ||
</head> | ||
<body> | ||
<a href="docs/index.html" style="color:#ccc;font-size:12px">Redirect to homepage</a> | ||
</body> | ||
</html> | ||
|
||
</body></html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import chalk from 'chalk'; | ||
import cheerio from 'cheerio'; | ||
|
||
export const processPages = async ({ | ||
pages, | ||
pageTableOfContentsEnabled, | ||
tableOfContents, | ||
mainTemplate, | ||
webCover | ||
}) => { | ||
console.log(chalk.green('Generating the static web content')); | ||
tableOfContents.forEach((section) => { | ||
section.pages.forEach((page) => { | ||
let $ = cheerio.load(mainTemplate.html()); //clone | ||
let key = page.source; | ||
let content = pages[key]; | ||
//add relevant container | ||
if (page.html === true) { | ||
//raw HTML pages should not be confined to the fixed width | ||
$('#dg-content').html('<div id="dg-innerContent"></div>'); | ||
} else { | ||
//Markdown pages should be confined to the fixed width | ||
$('#dg-content').html( | ||
'<div class="w-fixed-width"><div id="dg-innerContent"></div></div>', | ||
); | ||
} | ||
$('#dg-innerContent').html(content); | ||
//------------------------------------------------------------------------------------------------------ | ||
//insert permalinks for every page heading | ||
//when pageToc is enabled, also insert a page-level table of contents | ||
let html = [], | ||
i = -1; | ||
let headings = $('h1, h2, h3, h4, h5, h6'); | ||
if (headings.length > 0) { | ||
html[++i] = '<ul class="dg-pageToc">'; | ||
} | ||
headings.each(function () { | ||
let label = $(this).text(); | ||
let anchor = label.toLowerCase().replace(/\s+/g, '-'); | ||
$(this).attr('id', anchor); | ||
html[++i] = '<li><a href="#' + anchor + '">' + label + '</a></li>'; | ||
}); | ||
if (headings.length > 0) { | ||
html[++i] = '</ul>'; | ||
} | ||
if (pageTableOfContentsEnabled === true && page.html !== true) { | ||
$('#dg-innerContent').prepend(html.join('')); | ||
} | ||
//------------------------------------------------------------------------------------------------------ | ||
//prepend the auto heading (which makes the PDF table of contents match the web TOC) | ||
$('#dg-innerContent').prepend( | ||
'<h1 id="dg-autoTitle">' + page.title + '</h1>', | ||
); | ||
if (page.html === true) { | ||
$('#dg-autoTitle').addClass('dg-hiddenTitle'); | ||
} | ||
//------------------------------------------------------------------------------------------------------ | ||
//apply the w-table class | ||
$('table:not(.unstyled)').addClass('w-table w-fixed w-stripe'); | ||
//------------------------------------------------------------------------------------------------------ | ||
pages[key] = $; | ||
}); | ||
}); | ||
//add web ownership page | ||
let $ = cheerio.load(mainTemplate.html()); //clone | ||
$('#dg-content').html( | ||
'<div class="w-fixed-width"><div id="dg-innerContent"></div></div>', | ||
); | ||
$('#dg-innerContent').html(webCover.html()); | ||
return $; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import chalk from 'chalk'; | ||
import path from 'path'; | ||
import { writeFile } from '../fs/fs'; | ||
|
||
export let createRedirect = async ({ | ||
isRedirectEnabled, | ||
outputDirectory, | ||
redirectTemplate, | ||
homePage, | ||
verbose | ||
}) => { | ||
if (isRedirectEnabled) { | ||
let parent = outputDirectory.replace(/\/$/, ''); //trim any trailing slash | ||
parent = parent.split(path.sep).slice(-1).pop(); //get name of final directory in the path | ||
let homepage = homePage; | ||
homepage = | ||
homepage.source.substr(0, homepage.source.lastIndexOf('.')) + '.html'; | ||
let redirectLink = parent + '/' + homepage; | ||
let $ = redirectTemplate; | ||
$('a').attr('href', redirectLink); | ||
$('meta[http-equiv=REFRESH]').attr('content', '0;url=' + redirectLink); | ||
let file = outputDirectory + '../' + 'index.html'; | ||
try { | ||
await writeFile(file, $.html()); | ||
} catch (error) { | ||
console.log(chalk.red('Error writing redirect file: ' + file)); | ||
if (verbose === true) { | ||
console.log(chalk.red(error)); | ||
} | ||
//don't exit because redirect error is not a fatal error | ||
} | ||
} | ||
}; |
Oops, something went wrong.