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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 22x 22x 17x 17x 1x 1x 17x 17x 10x 10x 10x 17x 17x 4x 4x 17x 17x 13x 13x 10x 10x 10x 4x 4x 10x 13x 13x 17x 17x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 17x 1x 1x 7x | import { heading } from './elements/heading.js';
import { DocumentSection, tryFindSection } from './sections/section.js';
import { tableOfContentsSection } from './sections/table-of-contents.js';
export interface MarkdownDocumentOptions {
title: string;
description?: string;
content?: Array<DocumentSection>;
/**
* Include a table of contents in the output
*
* @default true
*/
tableOfContents?: boolean;
}
export interface AddSection {
section: DocumentSection;
sortOrder?: number;
}
export class MarkdownDocument {
private _tableOfContents: boolean;
private _sortedSections: Array<DocumentSection> = [];
private _title: string;
constructor(private options: MarkdownDocumentOptions) {
this._tableOfContents = options.tableOfContents ?? true;
this._title = options.title;
options.content?.forEach((section) => this.addSection(section));
return this;
}
private get sections(): ReadonlyArray<DocumentSection> {
return this._sortedSections;
}
public get title() {
return this._title;
}
public addSection(section: DocumentSection) {
this._sortedSections.push(section);
return this;
}
public tryFindSection<S extends DocumentSection>(path: Array<string>): S {
return tryFindSection(this.sections, path) as S;
}
private synthesizeSections(sections: ReadonlyArray<DocumentSection>) {
const lines: Array<string> = [];
for (const section of sections) {
lines.push(section.synthesize());
if (section.subSections.length > 0) {
lines.push(...this.synthesizeSections(section.subSections));
}
}
return lines;
}
public toString() {
let tableOfContents;
if (this._tableOfContents) {
tableOfContents = tableOfContentsSection({
sections: this.sections,
}).synthesize();
}
const lines = [
heading(1, this.options.title),
this.options.description,
tableOfContents,
'',
];
lines.push(...this.synthesizeSections(this.sections));
return lines.filter(Boolean).join('\n\n');
}
}
export const markdownDocument = (options: MarkdownDocumentOptions) =>
new MarkdownDocument(options);
|