mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-21 12:28:46 +00:00
feat(documentation): svelte app as document generator (#566)
* check in full site application * rm unused assets components * change base css * i18n * new doc workflow * fix crosslink to doc in github * nav, console link, assets, console brand * edit configjs server * rm go struct references * cleanup input output bindings * working dir * export path * always trigger * rel paths * cname * rev workdir * fix fallbacklanguage, home on large screens * remove mit from site
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export default function generate_code_tabs(dirpath, dir) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import fs from 'fs';
|
||||
import hljs from 'highlight.js';
|
||||
import marked from 'marked';
|
||||
import path from 'path';
|
||||
|
||||
import { SLUG_PRESERVE_UNICODE, SLUG_SEPARATOR } from '../../config';
|
||||
import { extract_frontmatter, extract_metadata, langs, link_renderer } from './markdown.js';
|
||||
import { make_session_slug_processor } from './slug';
|
||||
|
||||
const block_types = [
|
||||
'blockquote',
|
||||
'html',
|
||||
'heading',
|
||||
'hr',
|
||||
'list',
|
||||
'listitem',
|
||||
'paragraph',
|
||||
'table',
|
||||
'tablerow',
|
||||
'tablecell'
|
||||
];
|
||||
|
||||
export default function generate_docs(dirpath, dir, lang) {
|
||||
const make_slug = make_session_slug_processor({
|
||||
separator: SLUG_SEPARATOR,
|
||||
preserve_unicode: SLUG_PRESERVE_UNICODE
|
||||
});
|
||||
|
||||
console.log('using language: ' + lang);
|
||||
|
||||
return fs
|
||||
.readdirSync(`${dirpath}${dir}`)
|
||||
.filter((file) => {
|
||||
return file[0] !== '.' && path.extname(file) === '.md' && file.endsWith(`.${lang}.md`);
|
||||
})
|
||||
.map((file) => {
|
||||
const markdown = fs.readFileSync(`${dirpath}${dir}/${file}`, 'utf-8');
|
||||
const { content, metadata } = extract_frontmatter(markdown);
|
||||
const section_slug = make_slug(metadata.title);
|
||||
const subsections = [];
|
||||
|
||||
const renderer = new marked.Renderer();
|
||||
|
||||
let block_open = false;
|
||||
|
||||
renderer.link = link_renderer;
|
||||
|
||||
renderer.hr = (str) => {
|
||||
block_open = true;
|
||||
|
||||
return '<div class="side-by-side"><div class="copy">';
|
||||
};
|
||||
|
||||
// renderer.list = (src) => {
|
||||
// console.log(src);
|
||||
// };
|
||||
|
||||
renderer.code = (source, lang) => {
|
||||
source = source.replace(/^ +/gm, (match) => match.split(' ').join('\t'));
|
||||
|
||||
const lines = source.split('\n');
|
||||
|
||||
const meta = extract_metadata(lines[0], lang);
|
||||
|
||||
let prefix = '';
|
||||
// let class_name = 'code-block';
|
||||
let class_name = '';
|
||||
|
||||
if (meta) {
|
||||
source = lines.slice(1).join('\n');
|
||||
const filename = meta.filename || (lang === 'html' && 'App.svelte');
|
||||
if (filename) {
|
||||
prefix = `<span class='filename'>${prefix} ${filename}</span>`;
|
||||
class_name += ' named';
|
||||
}
|
||||
}
|
||||
|
||||
if (meta && meta.hidden) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const plang = langs[lang];
|
||||
const { value: highlighted } = hljs.highlight(lang, source);
|
||||
|
||||
const html = `<div class='${class_name}'>${prefix}<pre class='language-${plang}'><code>${highlighted}</code></pre></div>`;
|
||||
|
||||
if (block_open) {
|
||||
block_open = false;
|
||||
return `</div><div class="code">${html}</div></div>`;
|
||||
}
|
||||
|
||||
return html;
|
||||
};
|
||||
|
||||
// const slugger = new marked.Slugger();
|
||||
renderer.heading = (text, level, rawtext) => {
|
||||
const slug = level <= 4 && make_slug(rawtext);
|
||||
|
||||
if (level === 3 || level === 4) {
|
||||
const title = text.replace(/<\/?code>/g, '').replace(/\.(\w+)(\((.+)?\))?/, (m, $1, $2, $3) => {
|
||||
if ($3) return `.${$1}(...)`;
|
||||
if ($2) return `.${$1}()`;
|
||||
return `.${$1}`;
|
||||
});
|
||||
|
||||
subsections.push({ slug, title, level });
|
||||
}
|
||||
|
||||
return `
|
||||
<h${level}>
|
||||
<span id="${slug}" class="offset-anchor" ${level > 4 ? 'data-scrollignore' : ''}></span>
|
||||
<a href="${dir}#${slug}" class="anchor" aria-hidden="true"> <i class="las la-link"></i> </a>
|
||||
${text}
|
||||
</h${level}>`;
|
||||
};
|
||||
|
||||
block_types.forEach((type) => {
|
||||
const fn = renderer[type];
|
||||
renderer[type] = function () {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
const html = marked(content, { renderer });
|
||||
const hashes = {};
|
||||
|
||||
return {
|
||||
html: html.replace(/@@(\d+)/g, (m, id) => hashes[id] || m),
|
||||
metadata,
|
||||
subsections,
|
||||
slug: section_slug,
|
||||
file
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export default function extract_languages(dirpath, dir) {
|
||||
|
||||
const detectedLocales = fs.readdirSync(`${dirpath}${dir}`)
|
||||
.filter(file => path.extname(file) == '.md')
|
||||
.map((file) => {
|
||||
file = file.replace(path.extname(file), '');
|
||||
const arr = file.split('.');
|
||||
const locale = arr.length ? arr[arr.length - 1] : null;
|
||||
if (locale) {
|
||||
return locale;
|
||||
}
|
||||
}).filter(locale => locale !== null);
|
||||
|
||||
const redDetectedLocales = [...new Set(detectedLocales)];
|
||||
|
||||
console.log('detected locales: ' + redDetectedLocales);
|
||||
return redDetectedLocales;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import * as fleece from 'golden-fleece';
|
||||
|
||||
export function extract_frontmatter(markdown) {
|
||||
const match = /---\r?\n([\s\S]+?)\r?\n---/.exec(markdown);
|
||||
const frontMatter = match[1];
|
||||
const content = markdown.slice(match[0].length);
|
||||
|
||||
const metadata = {};
|
||||
frontMatter.split('\n').forEach((pair) => {
|
||||
const colonIndex = pair.indexOf(':');
|
||||
metadata[pair.slice(0, colonIndex).trim()] = pair.slice(colonIndex + 1).trim();
|
||||
});
|
||||
|
||||
return { metadata, content };
|
||||
}
|
||||
|
||||
export function extract_metadata(line, lang) {
|
||||
try {
|
||||
if (lang === 'html' && line.startsWith('<!--') && line.endsWith('-->')) {
|
||||
console.log(line);
|
||||
return fleece.evaluate(line.slice(4, -3).trim());
|
||||
}
|
||||
|
||||
if (lang === 'codeblock' && line.startsWith('<!--') && line.endsWith('-->')) {
|
||||
console.log(line);
|
||||
return fleece.evaluate(line.slice(4, -3).trim());
|
||||
}
|
||||
|
||||
if (lang === 'js' || (lang === 'json' && line.startsWith('/*') && line.endsWith('*/'))) {
|
||||
return fleece.evaluate(line.slice(2, -2).trim());
|
||||
}
|
||||
} catch (err) {
|
||||
// TODO report these errors, don't just squelch them
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// map lang to language-attributes
|
||||
export const langs = {
|
||||
bash: 'bash',
|
||||
html: 'markup',
|
||||
sv: 'markup',
|
||||
js: 'javascript',
|
||||
css: 'css'
|
||||
};
|
||||
|
||||
// links renderer
|
||||
export function link_renderer(href, title, text) {
|
||||
let target_attr = '';
|
||||
let title_attr = '';
|
||||
|
||||
if (href.startsWith('http')) {
|
||||
target_attr = ' target="_blank"';
|
||||
}
|
||||
|
||||
if (title !== null) {
|
||||
title_attr = ` title="${title}"`;
|
||||
}
|
||||
|
||||
return `<a href="${href}"${target_attr}${title_attr}>${text}</a>`;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import slugify from '@sindresorhus/slugify';
|
||||
|
||||
export const SLUG_PRESERVE_UNICODE = false;
|
||||
export const SLUG_SEPARATOR = '_';
|
||||
|
||||
/* url-safe processor */
|
||||
|
||||
export const urlsafeSlugProcessor = (string, opts) => {
|
||||
const { separator = SLUG_SEPARATOR } = opts || {};
|
||||
|
||||
return slugify(string, {
|
||||
customReplacements: [
|
||||
// runs before any other transformations
|
||||
['$', 'DOLLAR'], // `$destroy` & co
|
||||
['-', 'DASH'] // conflicts with `separator`
|
||||
],
|
||||
separator,
|
||||
decamelize: false,
|
||||
lowercase: false
|
||||
})
|
||||
.replace(/DOLLAR/g, '$')
|
||||
.replace(/DASH/g, '-');
|
||||
};
|
||||
|
||||
/* unicode-preserver processor */
|
||||
|
||||
const alphaNumRegex = /[a-zA-Z0-9]/;
|
||||
const unicodeRegex = /\p{Letter}/u;
|
||||
const isNonAlphaNumUnicode = (string) => !alphaNumRegex.test(string) && unicodeRegex.test(string);
|
||||
|
||||
export const unicodeSafeProcessor = (string, opts) => {
|
||||
const { separator = SLUG_SEPARATOR } = opts || {};
|
||||
|
||||
return string
|
||||
.split('')
|
||||
.reduce(
|
||||
(accum, char, index, array) => {
|
||||
const type = isNonAlphaNumUnicode(char) ? 'pass' : 'process';
|
||||
|
||||
if (index === 0) {
|
||||
accum.current = { type, string: char };
|
||||
} else if (type === accum.current.type) {
|
||||
accum.current.string += char;
|
||||
} else {
|
||||
accum.chunks.push(accum.current);
|
||||
accum.current = { type, string: char };
|
||||
}
|
||||
|
||||
if (index === array.length - 1) {
|
||||
accum.chunks.push(accum.current);
|
||||
}
|
||||
|
||||
return accum;
|
||||
},
|
||||
{ chunks: [], current: { type: '', string: '' } }
|
||||
)
|
||||
.chunks.reduce((accum, chunk) => {
|
||||
const processed = chunk.type === 'process' ? urlsafeSlugProcessor(chunk.string) : chunk.string;
|
||||
|
||||
processed.length > 0 && accum.push(processed);
|
||||
|
||||
return accum;
|
||||
}, [])
|
||||
.join(separator);
|
||||
};
|
||||
|
||||
/* session processor */
|
||||
|
||||
export const make_session_slug_processor = ({
|
||||
preserve_unicode = SLUG_PRESERVE_UNICODE,
|
||||
separator = SLUG_SEPARATOR
|
||||
}) => {
|
||||
const processor = preserve_unicode ? unicodeSafeProcessor : urlsafeSlugProcessor;
|
||||
const seen = new Set();
|
||||
|
||||
return (string) => {
|
||||
const slug = processor(string, { separator });
|
||||
|
||||
if (seen.has(slug)) throw new Error(`Duplicate slug ${slug}`);
|
||||
seen.add(slug);
|
||||
|
||||
return slug;
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user