{"version":3,"sources":["../src/markdown-document.ts"],"sourcesContent":["import { heading } from './elements/heading.js';\nimport { DocumentSection, tryFindSection } from './sections/section.js';\nimport { tableOfContentsSection } from './sections/table-of-contents.js';\n\nexport interface MarkdownDocumentOptions {\n  title: string;\n\n  description?: string;\n\n  content?: Array<DocumentSection>;\n\n  /**\n   * Include a table of contents in the output\n   *\n   * @default true\n   */\n  tableOfContents?: boolean;\n}\n\nexport interface AddSection {\n  section: DocumentSection;\n  sortOrder?: number;\n}\n\nexport class MarkdownDocument {\n  private _tableOfContents: boolean;\n  private _sortedSections: Array<DocumentSection> = [];\n\n  private _title: string;\n\n  constructor(private options: MarkdownDocumentOptions) {\n    this._tableOfContents = options.tableOfContents ?? true;\n    this._title = options.title;\n    options.content?.forEach((section) => this.addSection(section));\n    return this;\n  }\n\n  private get sections(): ReadonlyArray<DocumentSection> {\n    return this._sortedSections;\n  }\n\n  public get title() {\n    return this._title;\n  }\n\n  public addSection(section: DocumentSection) {\n    this._sortedSections.push(section);\n    return this;\n  }\n\n  public tryFindSection<S extends DocumentSection>(path: Array<string>): S {\n    return tryFindSection(this.sections, path) as S;\n  }\n\n  private synthesizeSections(sections: ReadonlyArray<DocumentSection>) {\n    const lines: Array<string> = [];\n    for (const section of sections) {\n      lines.push(section.synthesize());\n\n      if (section.subSections.length > 0) {\n        lines.push(...this.synthesizeSections(section.subSections));\n      }\n    }\n    return lines;\n  }\n\n  public toString() {\n    let tableOfContents;\n    if (this._tableOfContents) {\n      tableOfContents = tableOfContentsSection({\n        sections: this.sections,\n      }).synthesize();\n    }\n\n    const lines = [\n      heading(1, this.options.title),\n      this.options.description,\n      tableOfContents,\n      '',\n    ];\n\n    lines.push(...this.synthesizeSections(this.sections));\n\n    return lines.filter(Boolean).join('\\n\\n');\n  }\n}\n\nexport const markdownDocument = (options: MarkdownDocumentOptions) =>\n  new MarkdownDocument(options);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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