Skip to content

[FEATURE]: add rawdescription on miscellaneous symbols #1646

Closed

Description

@RobbyRabbitman

add rawdescription on miscellaneous symbols, so that you can use Markdown from @storybook/addon-docs/blocks. This enables to display docs for functions, variables etc. in the same way storybook already does with addon-docs. In my project I created a custom doc block like so:

import { Description } from 'my-package';
import { Meta } from '@storybook/addon-docs/blocks';

<Meta title="My Function" />

<Description symbol="myFunction" />
import { Markdown } from '@storybook/addon-docs/blocks';
import { getCompodocJson } from './get-compodoc-json';

interface DescriptionProps {
  symbol: string;
  compodoc?: unknown;
}

/** Renders the description of a symbol of a compodoc as markdown. */
export const Description: React.FC<DescriptionProps> = (props) => {
  const { symbol, compodoc = getCompodocJson() } = props;

  const entry = findEntry(compodoc, symbol);

  return <Markdown>{entry?.rawdescription ?? ''}</Markdown>;
};

const COMPODOC_SYMBOL_KINDS = [
  'injectables',
  'classes',
  'directives',
  'components',
  'pipes',
  'miscellaneous.functions',
] as const;

type CompodocEntryKind = (typeof COMPODOC_SYMBOL_KINDS)[number];

interface CompodocEntry {
  name: string;
  description: string;
  rawdescription: string;
}

const findEntry = (compodoc: unknown, symbol: string) => {
  for (const kind of COMPODOC_SYMBOL_KINDS) {
    const entry = resolveEntries(compodoc, kind).find((c) => symbol === c.name);

    if (entry) {
      return entry;
    }
  }

  for (const kind of COMPODOC_SYMBOL_KINDS) {
    const entry = resolveEntries(compodoc, kind).find((c) =>
      symbol.endsWith(c.name),
    );

    if (entry) {
      return entry;
    }
  }

  return undefined;
};

function resolveEntries(compodoc: unknown, kind: CompodocEntryKind) {
  return kind
    .split('.')
    .reduce(
      (obj, path) =>
        obj &&
        typeof obj === 'object' &&
        (obj as Record<string, unknown>)[path],
      compodoc,
    ) as CompodocEntry[];
}

Related issues

#949

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions