All files / src/sections examples.ts

100% Statements 123/123
100% Branches 20/20
100% Functions 6/6
100% Lines 123/123

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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1241x 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 8x 7x 7x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 7x 1x 1x 1x 7x 7x 8x 7x 7x 7x 8x 8x 8x 8x 1x 1x 8x 8x  
import { DocumentSection, DocumentSectionOptions } from './section.js';
import type { Optional } from '../@types/optional.js';
import { codeBlock } from '../elements/code-block.js';
import { HeadingLevel, heading } from '../elements/heading.js';
 
interface CodeBlock {
  language:
    | 'apache'
    | 'armasm'
    | 'bash'
    | 'c'
    | 'coffeescript'
    | 'cpp'
    | 'csharp'
    | 'css'
    | 'd'
    | 'diff'
    | 'go'
    | 'handlebars'
    | 'haskell'
    | 'http'
    | 'ini'
    | 'java'
    | 'javascript'
    | 'json'
    | 'julia'
    | 'kotlin'
    | 'less'
    | 'lua'
    | 'makefile'
    | 'markdown'
    | 'nginx'
    | 'objectivec'
    | 'perl'
    | 'php'
    | 'plaintext'
    | 'properties'
    | 'python'
    | 'r'
    | 'ruby'
    | 'rust'
    | 'scala'
    | 'scss'
    | 'shell'
    | 'sql'
    | 'swift'
    | 'typescript'
    | 'x86asm'
    | 'xml'
    | 'yaml';
  code: string;
}
 
export interface Example {
  title: string;
  description?: string;
  codeblock: CodeBlock;
  group?: string;
}
 
export interface ExamplesOptions
  extends Optional<DocumentSectionOptions, 'title'> {
  items?: Array<Example>;
}
export class Examples extends DocumentSection {
  protected items = new Map<string, Array<Example>>();
 
  constructor(options?: ExamplesOptions) {
    super({ title: options?.title ?? 'Examples', ...options });
    options?.items?.forEach((item) => this.add(item));
  }
 
  public add(item: Example) {
    const group = this.items.get(item.group ?? 'ungrouped');
 
    if (group) {
      group.push(item);
    } else {
      this.items.set(item.group ?? 'ungrouped', [item]);
    }
 
    return this;
  }
 
  protected itemMapper(item: Example, headingLevel: HeadingLevel): string {
    const { code, language } = item.codeblock;
 
    return [
      heading(headingLevel, item.title),
      ...(item.description ? ['', item.description, ''] : ['']),
      // `\`\`\`${item.codeblock.language}`,
      // item.codeblock.code.replace(/\n$/, ''),
      codeBlock({ code, language }),
      // item.codeblock.code.replace(/\n$/, ''),
      // '```',
      '',
    ].join('\n');
  }
 
  protected synthesizeContent(): Array<string> {
    const content = [heading(this.headingLevel, this.title)];
 
    let headingLevel = this.headingLevel + 1;
    for (const [group, examples] of this.items) {
      content.push('');
      if (group !== 'ungrouped') {
        content.push(heading(headingLevel as HeadingLevel, group), '');
        headingLevel++;
      }
      content.push(
        ...examples.map((item) =>
          this.itemMapper(item, headingLevel as HeadingLevel),
        ),
      );
    }
 
    return content;
  }
}
 
export function examplesSection(options?: ExamplesOptions) {
  return new Examples(options);
}