fleet/website/api/helpers/strings/to-html.js

168 lines
7.1 KiB
JavaScript
Raw Normal View History

Put live documentation on fleetdm.com (#1380) * minor clarifications * further expand comments and stubs * absorb custom titles embedded in metadata, plus further comment expansion and a followup fix for something i left hanging in f8cbc14829d91e7577c63307fd9c4346dbc229bb * Skip non-markdown files and use real path maths * Prep for running in parallel (Remove `continue` so this isn't dependent on the `for` loop) * determine + track unique HTML output paths * Compile markdown + spit out real HTML (without involving any but the crunchy nougaty dependency from the very center of everything) * add md metadata parsing * add timestamp * Update build-static-content.js * attach misc metadata as "other" * how doc images might should work (this also aligns with how the select few images in the sailsjs.com docs work) * add file extension to generated HTML files * "options"=>"meta" * Make "htmlId" useful for alphabetically sorting pages within their bottom-level section See recent comments on https://github.com/fleetdm/fleet/issues/706 for more information. * list out the most important, specific build-time transformations * Omit ordering prefixes like "1-" from expected content page URLs * add a little zone for consolidating backwards compatible permalinks * interpret README.md files by mapping their URLs to match their containing folder * clarify plan for images * decrease probability of collisions * Make capitalization smarter using known acronyms, proper nouns, and a smarter numeric word trim * Resolve app path in case pwd is different in prod * Delete HTML output from previous runs, if any * condense the stuff about github emojis * got rid of "permalink" thing, since id gets automatically attached during markdown compilation anyway Also "permalink" isn't even a good name for what this is. See https://github.com/fleetdm/fleet/issues/706#issuecomment-884693931 * …and that eliminates the need for the cheerio dep! * Bring in bubbles+syntax highlighting into build script, and remove sails.helpers.compileMarkdownContent() -- this leaves link munging as a todo though * trivial (condense comments) * Remove unused code from toHtml() helper * Implemented target="_blank" and root-relative-ification * remove todo about emojis after testing and verifying it works just fine * trivial: add link to comment in case github emojis matter at some point * consolidate "what ifs" in comments * Leave this up to Sarah, for now. (Either bring it back here in the build script or do it all on the frontend) * Enable /docs and /handbook routes, and add example of a redirect for a legacy/deprecated URL * implement routing * Upgrade deps this takes advantages of the latest work from @eashaw, @rachaelshaw, and the rest of the Sails community * tweak var names and comments * make readme pages use their folder names to determine their default (fallback) titles as discussed in https://github.com/fleetdm/fleet/issues/706#issuecomment-884788002 * first (good enough for now) pass at link rewriting as discussed in https://github.com/fleetdm/fleet/issues/706#issuecomment-884742072 * Adapt docs pages to build from markdown output * Continue work on docs pages * Add landing page * Remove unused code; minor changes * Replace regex * fixes https://github.com/fleetdm/fleet/pull/1380#issuecomment-891429581 * Don't rely on "path" being a global var * Syle fleetdm doc pages * Continue work on docs pages * Fix linting error * Disable lesshint style warnings * parasails-has-no-page-script attribute Added a parasails-has-no-page-script attribute to the docs template, added a check for that attribute in parasails.js and removed the empty page script for 498 * bring in latest parasails dep * trivial * Update links to dedupe and not open in new tab unless actually external * Disable handbook for now til styles are ready * fix CTA links * trivial * make sitemap.xml get served in prod * hide search boxes for now, remove hard-coded version and make releases open in new tab * clean out unused files Co-authored-by: gillespi314 <73313222+[email protected]> Co-authored-by: eashaw <[email protected]>
2021-08-18 00:55:13 +00:00
module.exports = {
friendlyName: 'To HTML',
description: 'Compile a Markdown string into an HTML string.',
extendedDescription:
'Expects GitHub-flavored Markdown syntax. Uses [`marked`](https://github.com/chjj/marked)@v0.3.5. '+
'Inspired by https://github.com/mikermcneil/machinepack-markdown/tree/5d8cee127e8ce45c702ec9bbb2b4f9bc4b7fafac',
moreInfoUrl: 'https://help.github.com/articles/basic-writing-and-formatting-syntax/',
sideEffects: 'cacheable',
inputs: {
mdString: {
description: 'Markdown string to convert',
example: '# hello world\n it\'s me, some markdown string \n\n ```js\n//but maybe i have code snippets too...\n```',
required: true
},
allowHtml: {
friendlyName: 'Allow HTML?',
description: 'Whether or not to allow HTML tags in the Markdown input. Defaults to `true`.',
extendedDescription: 'If `false`, any input that contains HTML tags will trigger the `unsafeMarkdown` exit.',
example: true,
defaultsTo: true
},
addIdsToHeadings: {
friendlyName: 'Add IDs to headings?',
description: 'Whether or not to add an ID attribute to rendered heading tags like <h1>',
extendedDescription: 'This is not part of the Markdown specification (see http://daringfireball.net/projects/markdown/dingus), but it is the default behavior for the `marked` module. Defaults to `true`.',
example: true,
defaultsTo: true
}
},
exits: {
success: {
outputFriendlyName: 'HTML',
outputExample: '<h1 id="hello-world">hello world</h1>\n<p> it&#39;s me, some markdown string </p>\n<pre><code class="lang-js">//but maybe i have code snippets too...</code></pre>\n'
},
unsafeMarkdown: {
friendlyName: 'Unsafe Markdown detected',
description: 'The provided input contained unsafe content (like HTML tags).'
}
},
fn: function(inputs, exits) {
const { marked } = require('marked');
Put live documentation on fleetdm.com (#1380) * minor clarifications * further expand comments and stubs * absorb custom titles embedded in metadata, plus further comment expansion and a followup fix for something i left hanging in f8cbc14829d91e7577c63307fd9c4346dbc229bb * Skip non-markdown files and use real path maths * Prep for running in parallel (Remove `continue` so this isn't dependent on the `for` loop) * determine + track unique HTML output paths * Compile markdown + spit out real HTML (without involving any but the crunchy nougaty dependency from the very center of everything) * add md metadata parsing * add timestamp * Update build-static-content.js * attach misc metadata as "other" * how doc images might should work (this also aligns with how the select few images in the sailsjs.com docs work) * add file extension to generated HTML files * "options"=>"meta" * Make "htmlId" useful for alphabetically sorting pages within their bottom-level section See recent comments on https://github.com/fleetdm/fleet/issues/706 for more information. * list out the most important, specific build-time transformations * Omit ordering prefixes like "1-" from expected content page URLs * add a little zone for consolidating backwards compatible permalinks * interpret README.md files by mapping their URLs to match their containing folder * clarify plan for images * decrease probability of collisions * Make capitalization smarter using known acronyms, proper nouns, and a smarter numeric word trim * Resolve app path in case pwd is different in prod * Delete HTML output from previous runs, if any * condense the stuff about github emojis * got rid of "permalink" thing, since id gets automatically attached during markdown compilation anyway Also "permalink" isn't even a good name for what this is. See https://github.com/fleetdm/fleet/issues/706#issuecomment-884693931 * …and that eliminates the need for the cheerio dep! * Bring in bubbles+syntax highlighting into build script, and remove sails.helpers.compileMarkdownContent() -- this leaves link munging as a todo though * trivial (condense comments) * Remove unused code from toHtml() helper * Implemented target="_blank" and root-relative-ification * remove todo about emojis after testing and verifying it works just fine * trivial: add link to comment in case github emojis matter at some point * consolidate "what ifs" in comments * Leave this up to Sarah, for now. (Either bring it back here in the build script or do it all on the frontend) * Enable /docs and /handbook routes, and add example of a redirect for a legacy/deprecated URL * implement routing * Upgrade deps this takes advantages of the latest work from @eashaw, @rachaelshaw, and the rest of the Sails community * tweak var names and comments * make readme pages use their folder names to determine their default (fallback) titles as discussed in https://github.com/fleetdm/fleet/issues/706#issuecomment-884788002 * first (good enough for now) pass at link rewriting as discussed in https://github.com/fleetdm/fleet/issues/706#issuecomment-884742072 * Adapt docs pages to build from markdown output * Continue work on docs pages * Add landing page * Remove unused code; minor changes * Replace regex * fixes https://github.com/fleetdm/fleet/pull/1380#issuecomment-891429581 * Don't rely on "path" being a global var * Syle fleetdm doc pages * Continue work on docs pages * Fix linting error * Disable lesshint style warnings * parasails-has-no-page-script attribute Added a parasails-has-no-page-script attribute to the docs template, added a check for that attribute in parasails.js and removed the empty page script for 498 * bring in latest parasails dep * trivial * Update links to dedupe and not open in new tab unless actually external * Disable handbook for now til styles are ready * fix CTA links * trivial * make sitemap.xml get served in prod * hide search boxes for now, remove hard-coded version and make releases open in new tab * clean out unused files Co-authored-by: gillespi314 <73313222+[email protected]> Co-authored-by: eashaw <[email protected]>
2021-08-18 00:55:13 +00:00
// For full list of options, see:
// • https://github.com/chjj/marked
var markedOpts = {
gfm: true,
tables: true,
breaks: false,
pedantic: false,
smartLists: true,
smartypants: false,
};
let customRenderer = new marked.Renderer();
if (inputs.addIdsToHeadings === true) {
var headingsRenderedOnThisPage = [];
customRenderer.heading = function (text, level) {
// If the heading has underscores and no spaces (e.g. osquery_async_host_collect_log_stats_interval) we'll add optional linebreaks before each underscore
var textWithLineBreaks;
if(text.match(/\S(\w+\_\S)+(\w\S)+/g) && !text.match(/\s/g)){
textWithLineBreaks = text.replace(/(\_)/g, '&#8203;_');
}
var headingID = _.kebabCase(_.unescape(text.toLowerCase()).replace(/[\\']/g, ''));
if(!_.contains(headingsRenderedOnThisPage, headingID)){
headingsRenderedOnThisPage.push(headingID);
} else {
headingID = sails.helpers.strings.ensureUniq(headingID, headingsRenderedOnThisPage);
headingsRenderedOnThisPage.push(headingID);
}
return '<h'+level+' class="markdown-heading" id="'+headingID+'">'+(textWithLineBreaks ? textWithLineBreaks : text)+'<a href="#'+headingID+'" class="markdown-link"></a></h'+level+'>\n';
};
} else {
customRenderer.heading = function (text, level) {
var textWithLineBreaks;
if(text.match(/\S(\w+\_\S)+(\w\S)+/g) && !text.match(/\s/g)){
textWithLineBreaks = text.replace(/(\_)/g, '&#8203;_');
}
return '<h'+level+'>'+(textWithLineBreaks ? textWithLineBreaks : text)+'</h'+level+'>';
Put live documentation on fleetdm.com (#1380) * minor clarifications * further expand comments and stubs * absorb custom titles embedded in metadata, plus further comment expansion and a followup fix for something i left hanging in f8cbc14829d91e7577c63307fd9c4346dbc229bb * Skip non-markdown files and use real path maths * Prep for running in parallel (Remove `continue` so this isn't dependent on the `for` loop) * determine + track unique HTML output paths * Compile markdown + spit out real HTML (without involving any but the crunchy nougaty dependency from the very center of everything) * add md metadata parsing * add timestamp * Update build-static-content.js * attach misc metadata as "other" * how doc images might should work (this also aligns with how the select few images in the sailsjs.com docs work) * add file extension to generated HTML files * "options"=>"meta" * Make "htmlId" useful for alphabetically sorting pages within their bottom-level section See recent comments on https://github.com/fleetdm/fleet/issues/706 for more information. * list out the most important, specific build-time transformations * Omit ordering prefixes like "1-" from expected content page URLs * add a little zone for consolidating backwards compatible permalinks * interpret README.md files by mapping their URLs to match their containing folder * clarify plan for images * decrease probability of collisions * Make capitalization smarter using known acronyms, proper nouns, and a smarter numeric word trim * Resolve app path in case pwd is different in prod * Delete HTML output from previous runs, if any * condense the stuff about github emojis * got rid of "permalink" thing, since id gets automatically attached during markdown compilation anyway Also "permalink" isn't even a good name for what this is. See https://github.com/fleetdm/fleet/issues/706#issuecomment-884693931 * …and that eliminates the need for the cheerio dep! * Bring in bubbles+syntax highlighting into build script, and remove sails.helpers.compileMarkdownContent() -- this leaves link munging as a todo though * trivial (condense comments) * Remove unused code from toHtml() helper * Implemented target="_blank" and root-relative-ification * remove todo about emojis after testing and verifying it works just fine * trivial: add link to comment in case github emojis matter at some point * consolidate "what ifs" in comments * Leave this up to Sarah, for now. (Either bring it back here in the build script or do it all on the frontend) * Enable /docs and /handbook routes, and add example of a redirect for a legacy/deprecated URL * implement routing * Upgrade deps this takes advantages of the latest work from @eashaw, @rachaelshaw, and the rest of the Sails community * tweak var names and comments * make readme pages use their folder names to determine their default (fallback) titles as discussed in https://github.com/fleetdm/fleet/issues/706#issuecomment-884788002 * first (good enough for now) pass at link rewriting as discussed in https://github.com/fleetdm/fleet/issues/706#issuecomment-884742072 * Adapt docs pages to build from markdown output * Continue work on docs pages * Add landing page * Remove unused code; minor changes * Replace regex * fixes https://github.com/fleetdm/fleet/pull/1380#issuecomment-891429581 * Don't rely on "path" being a global var * Syle fleetdm doc pages * Continue work on docs pages * Fix linting error * Disable lesshint style warnings * parasails-has-no-page-script attribute Added a parasails-has-no-page-script attribute to the docs template, added a check for that attribute in parasails.js and removed the empty page script for 498 * bring in latest parasails dep * trivial * Update links to dedupe and not open in new tab unless actually external * Disable handbook for now til styles are ready * fix CTA links * trivial * make sitemap.xml get served in prod * hide search boxes for now, remove hard-coded version and make releases open in new tab * clean out unused files Co-authored-by: gillespi314 <73313222+[email protected]> Co-authored-by: eashaw <[email protected]>
2021-08-18 00:55:13 +00:00
};
}
// Creating a custom codeblock renderer function to add syntax highlighting keywords and render mermaid code blocks (```mermaid```) without the added <pre> tags.
customRenderer.code = function(code, infostring) {
if(infostring === 'mermaid') {
return `<code class="mermaid">${_.escape(code)}</code>`;
} else if(infostring === 'js') {// Interpret `js` as `javascript`
return `<pre><code class="hljs javascript" v-pre>${_.escape(code)}</code></pre>`;
} else if(infostring === 'bash' || infostring === 'sh') {// Interpret `sh` and `bash` as `bash`
return `<pre><code class="hljs bash" v-pre>${_.escape(code)}</code></pre>`;
} else if(infostring !== '') {// leaving the code language as-is if the infoString is anything else.
return `<pre><code class="hljs ${_.escape(infostring)}" v-pre>${_.escape(code)}</code></pre>`;
} else {// When unspecified, default to `text`
return `<pre><code class="nohighlight" v-pre>${_.escape(code)}</code></pre>`;
}
};
// Creating a custom blockquote renderer function to render blockquotes as tip blockquotes.
customRenderer.blockquote = function(blockquoteHtml) {
return `<blockquote purpose="tip"><img src="/images/[email protected]" alt="An icon indicating that this section has important information"><div class="d-block">\n${blockquoteHtml}\n</div></blockquote>`;
};
// Custom renderer function to enable checkboxes in Markdown lists.
customRenderer.listitem = function(innerHtml, hasCheckbox, isChecked) {
if(!hasCheckbox){ // « If a list item does not have a checkbox, we'll render it normally.
return `<li>${innerHtml}</li>`;
} else if(isChecked) {// If this checkbox was checked in Markdown (- [x]), we'll add a disabled checked checkbox, and hide the original checkbox with CSS
return `<li purpose="checklist-item"><input disabled type="checkbox" checked><span purpose="task">${innerHtml}</span></li>`;
} else {// If the checkbox was not checked, we'll add a non-checked disabled checkbox, and hide the original checkbox with CSS.
return `<li purpose="checklist-item"><input disabled type="checkbox"><span purpose="task">${innerHtml}</span></li>`;
}
};
// Creating a custom table renderer to add Bootstrap's responsive table styles to markdown tables.
customRenderer.table = function(headerHtml, bodyHtml) {
return `<div class="table-responsive"><table class="table">\n<thead>\n${headerHtml}\n</thead>\n<tbody>${bodyHtml}\n</tbody>\n</table>\n</div>`;
};
markedOpts.renderer = customRenderer;
Put live documentation on fleetdm.com (#1380) * minor clarifications * further expand comments and stubs * absorb custom titles embedded in metadata, plus further comment expansion and a followup fix for something i left hanging in f8cbc14829d91e7577c63307fd9c4346dbc229bb * Skip non-markdown files and use real path maths * Prep for running in parallel (Remove `continue` so this isn't dependent on the `for` loop) * determine + track unique HTML output paths * Compile markdown + spit out real HTML (without involving any but the crunchy nougaty dependency from the very center of everything) * add md metadata parsing * add timestamp * Update build-static-content.js * attach misc metadata as "other" * how doc images might should work (this also aligns with how the select few images in the sailsjs.com docs work) * add file extension to generated HTML files * "options"=>"meta" * Make "htmlId" useful for alphabetically sorting pages within their bottom-level section See recent comments on https://github.com/fleetdm/fleet/issues/706 for more information. * list out the most important, specific build-time transformations * Omit ordering prefixes like "1-" from expected content page URLs * add a little zone for consolidating backwards compatible permalinks * interpret README.md files by mapping their URLs to match their containing folder * clarify plan for images * decrease probability of collisions * Make capitalization smarter using known acronyms, proper nouns, and a smarter numeric word trim * Resolve app path in case pwd is different in prod * Delete HTML output from previous runs, if any * condense the stuff about github emojis * got rid of "permalink" thing, since id gets automatically attached during markdown compilation anyway Also "permalink" isn't even a good name for what this is. See https://github.com/fleetdm/fleet/issues/706#issuecomment-884693931 * …and that eliminates the need for the cheerio dep! * Bring in bubbles+syntax highlighting into build script, and remove sails.helpers.compileMarkdownContent() -- this leaves link munging as a todo though * trivial (condense comments) * Remove unused code from toHtml() helper * Implemented target="_blank" and root-relative-ification * remove todo about emojis after testing and verifying it works just fine * trivial: add link to comment in case github emojis matter at some point * consolidate "what ifs" in comments * Leave this up to Sarah, for now. (Either bring it back here in the build script or do it all on the frontend) * Enable /docs and /handbook routes, and add example of a redirect for a legacy/deprecated URL * implement routing * Upgrade deps this takes advantages of the latest work from @eashaw, @rachaelshaw, and the rest of the Sails community * tweak var names and comments * make readme pages use their folder names to determine their default (fallback) titles as discussed in https://github.com/fleetdm/fleet/issues/706#issuecomment-884788002 * first (good enough for now) pass at link rewriting as discussed in https://github.com/fleetdm/fleet/issues/706#issuecomment-884742072 * Adapt docs pages to build from markdown output * Continue work on docs pages * Add landing page * Remove unused code; minor changes * Replace regex * fixes https://github.com/fleetdm/fleet/pull/1380#issuecomment-891429581 * Don't rely on "path" being a global var * Syle fleetdm doc pages * Continue work on docs pages * Fix linting error * Disable lesshint style warnings * parasails-has-no-page-script attribute Added a parasails-has-no-page-script attribute to the docs template, added a check for that attribute in parasails.js and removed the empty page script for 498 * bring in latest parasails dep * trivial * Update links to dedupe and not open in new tab unless actually external * Disable handbook for now til styles are ready * fix CTA links * trivial * make sitemap.xml get served in prod * hide search boxes for now, remove hard-coded version and make releases open in new tab * clean out unused files Co-authored-by: gillespi314 <73313222+[email protected]> Co-authored-by: eashaw <[email protected]>
2021-08-18 00:55:13 +00:00
// Now actually compile the markdown to HTML.
marked(inputs.mdString, markedOpts, function afterwards (err, htmlString) {
if (err) { return exits.error(err); }
// If we're not allowing HTML, compile the input again with the `sanitize` option on.
if (inputs.allowHtml === false) {
markedOpts.sanitize = true;
marked(inputs.mdString, markedOpts, function sanitized (err, sanitizedHtmlString) {
if (err) { return exits.error(err); }
// Now compare the unsanitized and the sanitized output, and if they're not the same,
// leave through the `unsafeMarkdown` exit since it means that HTML tags were detected.
if (htmlString !== sanitizedHtmlString) {
return exits.unsafeMarkdown();
}
return exits.success(htmlString);
});
}
else {
return exits.success(htmlString);
}
});
}
};