All files write.js

4.17% Statements 1/24
0% Branches 0/20
0% Functions 0/4
4.17% Lines 1/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                  1x                                                                                                
import fs from "fs-extra";
import path from "path";
import marked from "marked";
 
import {getLanguage} from "./languages";
 
// Once all of the code has finished highlighting, we can **write** the resulting
// documentation file by passing the completed HTML sections into the template,
// and rendering it to the specified output path.
export const write = function(source, sections, config) {
	const destination = function(file) {
		const lang = getLanguage(source, config);
		const ext = lang && lang.name === "markdown" ? "md" : "html";
		return path.join(config.output, path.dirname(file), `${path.basename(file, path.extname(file)) }.${ext}`);
	};
	const relative = function(file) {
		const to = path.dirname(path.resolve(file));
		const from = path.dirname(path.resolve(destination(source)));
		return path.join(path.relative(from, to), path.basename(file));
	};
	// The **title** of the file is either the first heading in the prose, or the
	// name of the source file.
	const firstSection = sections.find(function(section) {
		return section.docsText && section.docsText.length > 0;
	});
	const first = firstSection ? marked.lexer(firstSection.docsText)[0] : {};
	const hasTitle = first.type === "heading" && first.depth === 1;
	const title = hasTitle ? first.text : path.basename(source);
	const css = relative(path.join(config.output, path.basename(config.css)));
	const language = getLanguage(source, config);
	const sources = config.sources;
 
	// If we have a template function then use it otherwise we'll have no output.
	const output = typeof config.template === "function" ? config.template({
		css,
		destination,
		hasTitle,
		language,
		path,
		relative,
		sections,
		sources,
		title
	}) : false;
 
	if (output) {
		if (config.verbose) {
			global.console.log(`docco: ${source} -> ${destination(source)}`);
		}
		if (!config.dryrun) {
			return fs.outputFileSync(destination(source), output);
		}
	}
	return false;
};
 
export default write;