fleet/website/api/controllers/download-sitemap.js
Mike McNeil 8097251565
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+gillespi314@users.noreply.github.com>
Co-authored-by: eashaw <caglc@live.com>
2021-08-17 19:55:13 -05:00

83 lines
5.4 KiB
JavaScript
Vendored

module.exports = {
friendlyName: 'Download sitemap',
description: 'Download sitemap file (returning a stream).',
extendedDescription: `Notes:
• Sitemap building inspired by https://github.com/sailshq/sailsjs.com/blob/b53c6e6a90c9afdf89e5cae00b9c9dd3f391b0e7/api/controllers/documentation/refresh.js#L112-L180 and https://github.com/sailshq/sailsjs.com/blob/b53c6e6a90c9afdf89e5cae00b9c9dd3f391b0e7/api/helpers/get-pages-for-sitemap.js
• Why escape XML? See http://stackoverflow.com/questions/3431280/validation-problem-entityref-expecting-what-should-i-do and https://github.com/sailshq/sailsjs.com/blob/b53c6e6a90c9afdf89e5cae00b9c9dd3f391b0e7/api/controllers/documentation/refresh.js#L161-L172
`,
exits: {
success: { outputFriendlyName: 'Sitemap (XML)', outputType: 'string' },
badConfig: { responseType: 'badConfig' },
},
fn: async function ({}) {
if (sails.config.environment === 'staging') {
// This explicit check for staging allows for the sitemap to still be developed/tested locally,
// and for the real thing to be served in production, while explicitly preventing the "whoops,
// i deployed staging and search engine crawlers got fixated on the wrong sitemap" dilemma.
throw new Error('Since this is the staging environment, prevented sitemap.xml from being served to avoid search engine accidents.');
}
if (!_.isObject(sails.config.builtStaticContent)) {
throw {badConfig: 'builtStaticContent'};
} else if (!_.isArray(sails.config.builtStaticContent.queries)) {
throw {badConfig: 'builtStaticContent.queries'};
} else if (!_.isArray(sails.config.builtStaticContent.markdownPages)) {
throw {badConfig: 'builtStaticContent.markdownPages'};
}
// Start with sitemap.xml preamble + the root relative URLs of other webpages that aren't being generated from markdown
let sitemapXml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ╦ ╦╔═╗╔╗╔╔╦╗ ╔═╗╔═╗╔╦╗╔═╗╔╦╗ ╔═╗╔═╗╔═╗╔═╗╔═╗
// ╠═╣╠═╣║║║ ║║───║ ║ ║ ║║║╣ ║║ ╠═╝╠═╣║ ╦║╣ ╚═╗
// ╩ ╩╩ ╩╝╚╝═╩╝ ╚═╝╚═╝═╩╝╚═╝═╩╝ ╩ ╩ ╩╚═╝╚═╝╚═╝
let HAND_CODED_HTML_PAGES = [
'/',
'/get-started',
'/company/contact',
'/queries',
// FUTURE: Do something smarter to get hand-coded HTML pages from routes.js, like how rebuild-cloud-sdk works, to avoid this manual duplication.
// See also https://github.com/sailshq/sailsjs.com/blob/b53c6e6a90c9afdf89e5cae00b9c9dd3f391b0e7/api/helpers/get-pages-for-sitemap.js#L27
];
for (let url of HAND_CODED_HTML_PAGES) {
let trimmedRootRelativeUrl = _.trimRight(url,'/');// « really only necessary for home page; run on everything as a failsafe against accidental dupes due to trailing slashes in the list above
sitemapXml += `<url><loc>${_.escape(sails.config.custom.baseUrl+trimmedRootRelativeUrl)}</loc></url>`;
}//∞
// ╔╦╗╦ ╦╔╗╔╔═╗╔╦╗╦╔═╗ ╔═╗╔═╗╦═╗ ╔═╗ ╦ ╦╔═╗╦═╗╦ ╦ ╔═╗╔═╗╔═╗╔═╗╔═╗
// ║║╚╦╝║║║╠═╣║║║║║ ╠═╝║╣ ╠╦╝───║═╬╗║ ║║╣ ╠╦╝╚╦╝ ╠═╝╠═╣║ ╦║╣ ╚═╗
// ═╩╝ ╩ ╝╚╝╩ ╩╩ ╩╩╚═╝ ╩ ╚═╝╩╚═ ╚═╝╚╚═╝╚═╝╩╚═ ╩ ╩ ╩ ╩╚═╝╚═╝╚═╝
for (let query of sails.config.builtStaticContent.queries) {
sitemapXml +=`<url><loc>${_.escape(sails.config.custom.baseUrl+`/queries/${query.slug}`)}</loc></url>`;// note we omit lastmod for some sitemap entries. This is ok, to mix w/ other entries that do have lastmod. Why? See https://docs.google.com/document/d/1SbpSlyZVXWXVA_xRTaYbgs3750jn252oXyMFLEQxMeU/edit
}//∞
// ╔╦╗╦ ╦╔╗╔╔═╗╔╦╗╦╔═╗ ╔═╗╔═╗╔═╗╔═╗╔═╗ ╔═╗╦═╗╔═╗╔╦╗ ╔╦╗╔═╗╦═╗╦╔═╔╦╗╔═╗╦ ╦╔╗╔
// ║║╚╦╝║║║╠═╣║║║║║ ╠═╝╠═╣║ ╦║╣ ╚═╗ ╠╣ ╠╦╝║ ║║║║ ║║║╠═╣╠╦╝╠╩╗ ║║║ ║║║║║║║
// ═╩╝ ╩ ╝╚╝╩ ╩╩ ╩╩╚═╝ ╩ ╩ ╩╚═╝╚═╝╚═╝ ╚ ╩╚═╚═╝╩ ╩ ╩ ╩╩ ╩╩╚═╩ ╩═╩╝╚═╝╚╩╝╝╚╝
if (sails.config.environment === 'development') {
for (let pageInfo of sails.config.builtStaticContent.markdownPages) {
sitemapXml +=`<url><loc>${_.escape(sails.config.custom.baseUrl+pageInfo.url)}</loc><lastmod>${_.escape(new Date(pageInfo.lastModifiedAt).toJSON())}</lastmod></url>`;
}//∞
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sitemapXml += '</urlset>';
// Set MIME type for content-type response header.
this.res.type('text/xml');
// Respond with XML.
return sitemapXml;
}
};