All files format.js

31.58% Statements 6/19
50% Branches 5/10
50% Functions 1/2
31.58% Lines 6/19

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                                1x 1x   1x     1x       1x                     1x                                    
// Formatter
// =========
// For each section of code or documentation we want to format it.
// Markdown is processed by Marked and code is (if possible) processed
// by Highlight.JS
//
// First the external dependancies...
import highlightjs from "highlight.js";
import marked from "marked";
 
// The one internal dependancy we have is language related.
import {getLanguage} from "./languages";
 
// To **format** and highlight the now-parsed sections of code, we use **Highlight.js**
// over stdio, and run the text of their corresponding comments through
// **Markdown**, using [Marked](https://github.com/chjj/marked).
export const format = function(source = "", sections = [], config = {}) {
	const language = getLanguage(source, config);
	// Pass any user defined options to Marked if specified via command line option
	const markedOptions = config.marked || {
		smartypants: true
	};
	marked.setOptions(markedOptions);
	// Tell Marked how to highlight code blocks within comments, treating that code
	// as either the language specified in the code block or the language of the file
	// if not specified.
	marked.setOptions({
		highlight(code, lang = language.name) {
			if (highlightjs.getLanguage(lang)) {
				return highlightjs.highlight(lang, code).value;
			}
			global.console.warn(`docco: couldn't highlight code block with unknown language '${lang}' in ${source}`);
			return code;
		}
	});
 
	// making sure this is an immutable operation by cloning sections.
	return [...sections].map((section) => {
		let code;
		try {
			code = highlightjs.highlight(language.name, section.codeText).value;
		} catch (err) {
			if (config.throw) {
				throw err;
			}
			code = section.codeText;
		}
		code = code.replace(/\s+$/, "");
		section.codeHtml = `<div class='highlight'><pre>${code}</pre></div>`;
		section.docsHtml = marked(section.docsText);
		return section;
	});
};
 
export default format;