Files
privacy.sexy/src/application/Parser/DocumentationParser.ts
undergroundwires 6067bdb24e Improve documentation support with markdown
Rework documentation URLs as inline markdown.

Redesign documentations with markdown text.

Redesign way to document scripts/categories and present the
documentation.

Documentation is showed in an expandable box instead of tooltip. This is
to allow writing longer documentation (tooltips are meant to be used for
short text) and have better experience on mobile.

If a node (script/category) has documentation it's now shown with single
information icon (ℹ) aligned to right.

Add support for rendering documentation as markdown. It automatically
converts plain URLs to URLs with display names (e.g.
https://docs.microsoft.com/..) will be rendered automatically like
"docs.microsoft.com - Windows 11 Privacy...".
2022-09-25 23:25:43 +02:00

59 lines
1.3 KiB
TypeScript

import type { DocumentableData, DocumentationData } from '@/application/collections/';
export function parseDocs(documentable: DocumentableData): readonly string[] {
if (!documentable) {
throw new Error('missing documentable');
}
const { docs } = documentable;
if (!docs) {
return [];
}
let result = new DocumentationContainer();
result = addDocs(docs, result);
return result.getAll();
}
function addDocs(
docs: DocumentationData,
container: DocumentationContainer,
): DocumentationContainer {
if (docs instanceof Array) {
if (docs.length > 0) {
container.addParts(docs);
}
} else if (typeof docs === 'string') {
container.addPart(docs);
} else {
throwInvalidType();
}
return container;
}
class DocumentationContainer {
private readonly parts = new Array<string>();
public addPart(documentation: string) {
if (!documentation) {
throw Error('missing documentation');
}
if (typeof documentation !== 'string') {
throwInvalidType();
}
this.parts.push(documentation);
}
public addParts(parts: readonly string[]) {
for (const part of parts) {
this.addPart(part);
}
}
public getAll(): ReadonlyArray<string> {
return this.parts;
}
}
function throwInvalidType() {
throw new Error('docs field (documentation) must be an array of strings');
}