Skip to content

@smitejs/snippets ​

Generate docs, JSDoc examples, and code from tried-and-tested #section snippets.

bash
yarn add @smitejs/snippets

16 documented symbols · version 2.0.0-SNAPSHOT.0 · source

Inline ​

constexpandExamples ​

ts
const expandExamples: (source: string, snippetIndex: ReadonlyMap<string, Snippet>, packageName: string, filePath: string) => string;

Expands every JSDoc @example <Title> line in a source string with the matching tested snippet, rendered as a fenced code block. Throws when a referenced snippet is missing. The code must not contain a closing JSDoc comment sequence (asterisk followed by a slash), which would close the comment early.

ts
const source = " * @example Expand @example declarations";
const snippet = buildSnippetIndex(
  [
    {
      title: "Expand @example declarations",
      code: "x()",
      slug: "x",
      filePath: "self.test.ts",
      startLine: 1,
      expected: null,
    },
  ],
  "self",
);
const expanded = expandExamples(source, snippet, "self", "self.d.ts");
ts
"```ts"

packages/snippets/src/expand.ts:18

constinjectIntoFiles ​

ts
const injectIntoFiles: (input: {
    readonly targetDir: string;
    readonly snippetIndex: ReadonlyMap<string, Snippet>;
    readonly packageName: string;
}) => Promise<readonly string[]>;

Walks targetDir for .d.ts/.js/.ts files and expands @example declarations in place against the snippet index. Returns the written paths.

packages/snippets/src/inject.ts:7

Markdown ​

constexpandMarkdown ​

ts
const expandMarkdown: (source: string, snippetIndex: ReadonlyMap<string, Snippet>, packageName: string, filePath: string) => {
    body: string;
    examples: readonly MarkdownInjectable[];
};

Scans a concept-document markdown source for @example <Title> lines, replacing them with placeholder lines and returning the list of injectables. The caller renders the placeholders to HTML after markdown conversion so code fences survive the renderer.

packages/snippets/src/expand.ts:125

interfaceMarkdownInjectable ​

ts
interface MarkdownInjectable {
    readonly placeholder: string;
    readonly snippet: Snippet;
}

A placeholder/render pair produced by expandMarkdownExamples. The caller scans a rendered document for each placeholder and replaces it with the rendered snippet markup.

packages/snippets/src/expand.ts:4

Reference ​

constcollectFiles ​

ts
const collectFiles: (directory: string, predicate: (filePath: string) => boolean) => Promise<readonly string[]>;

A folder of files matching a predicate, sorted by path.

packages/snippets/src/snippets.ts:26

constDEFAULT_INJECT_TARGET ​

ts
const DEFAULT_INJECT_TARGET: string;

Default target directory when none is supplied to the inject command.

packages/snippets/src/inject.ts:45

constnormalizeExampleName ​

ts
const normalizeExampleName: (value: string) => string;

Normalizes a snippet title into its lookup key.

packages/snippets/src/snippets.ts:246

constrenderExample ​

ts
const renderExample: (prefix: string, code: string) => string;

Renders a snippet into a JSDoc fenced ```ts block with the given prefix.

packages/snippets/src/expand.ts:73

constslugify ​

ts
const slugify: (value: string) => string;

Converts a snippet title into a URL-safe slug.

packages/snippets/src/snippets.ts:250

consttransformExpectedToResult ​

ts
const transformExpectedToResult: (code: string) => string;

Reduces a block of expect(...).toBe(...) assertions into just the expected values. Falls back to the raw code when no expect calls are present.

packages/snippets/src/snippets.ts:276

Snippets ​

constbuildSnippetIndex ​

ts
const buildSnippetIndex: (snippets: readonly Snippet[], packageName: string) => ReadonlyMap<string, Snippet>;

Builds the title → snippet index, throwing on duplicate or empty titles.

packages/snippets/src/snippets.ts:220

constcollectTestSnippets ​

ts
const collectTestSnippets: (input: {
    readonly packageName: string;
    readonly rootDir: string;
    readonly srcDir: string;
}) => Promise<{
    snippetIndex: ReadonlyMap<string, Snippet>;
    snippets: readonly Snippet[];
}>;

Collects every tested snippet from the *.test.ts files under srcDir, returning both the flat list and a title index. Duplicate titles throw.

packages/snippets/src/snippets.ts:181

constextractSnippetExpected ​

ts
const extractSnippetExpected: (testSource: string, snippet: Snippet) => string | null;

Extracts the expected-result lines that immediately follow a snippet's #endsection marker (the assertions testing it), as plain code. Returns null when there is nothing to show.

packages/snippets/src/snippets.ts:133

constextractSnippets ​

ts
const extractSnippets: (source: string, filePath: string) => readonly Snippet[];

Extracts every #section ... #endsection snippet from a test source. Nested or unclosed sections throw.

packages/snippets/src/snippets.ts:65

interfaceSnippet ​

ts
interface Snippet {
    readonly code: string;
    readonly filePath: string;
    readonly startLine: number;
    readonly title: string;
    readonly slug: string;
    readonly expected: string | null;
}

A tested documentation snippet extracted from a #section ... #endsection block. The snippet code is exercised by the test file it lives in, so it is safe to publish into docs, JSDoc declarations, or generated code.

packages/snippets/src/snippets.ts:4

Template ​

constexpandTemplate ​

ts
const expandTemplate: (source: string, snippetIndex: ReadonlyMap<string, Snippet>, packageName: string, filePath: string) => string;

Expands // @snippet <Title> marker lines in a template/code file by replacing each with the tested snippet code. The route either keeps files untouched when they carry no markers. Throws on unresolved markers.

ts
const snippet = buildSnippetIndex([
  {
    title: "Expand @snippet into a source file",
    code: "line = 1",
    slug: "x",
    filePath: "self.test.ts",
    startLine: 1,
    expected: null,
  },
]);
const expanded = expandTemplate(
  "// @snippet Expand @snippet into a source file\n",
  snippet,
  "self",
  "x.ts",
);
ts
"line = 1"

packages/snippets/src/expand.ts:82

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•api/smitejs-snippets