0.1.0 • Published 8 months ago

@blockquote/to-markdown v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@blockquote/to-markdown

Individual NPM package from the following PR

Custom-elements.json is a file format that describes custom elements. This format will allow tooling and IDEs to give rich information about the custom elements in a given project. It is, however, very experimental and things are subject to change. Follow the discussion here.

This library takes a Custom Elements Manifest and renders it to markdown.

Usage

Install:

npm i -S @blockquote/to-markdown

Import and use in your code:

import fs from 'fs';
import { customElementsManifestToMarkdown } from '@blockquote/to-markdown';

const manifest = JSON.parse(fs.readFileSync('./custom-elements.json', 'utf-8'));
const markdown = customElementsManifestToMarkdown(manifest);

fs.writeFileSync('./custom-elements.md', markdown);

Options

OptionTypeDefaultDescription
headingOffsetInteger0Offset the heading level by this number
mainDescriptionBooleantrueShow description field for Class and Mixins
private'all'\|'details'\|'hidden''all'See Private Members
omitDeclarationsOptionalDeclarations[][]See Omit Declarations
omitSectionsOptionalSections[][]See Omit Sections
classNameFilterstring \| (() => string)'.*'See Class Name Filter

Private Members

The private option controls how private members appear in the markdown.

  • 'all': private members appear alongside public members according to source order
  • 'hidden': private members do not appear at all in markdown, but protected members do
  • 'details': private and protected members appear in a details disclosure widget below the table

Omit Declarations

The omitDeclarations option is a string[] that controls which kinds of entities are rendered in the final markdown output. The four declaration types are:

  • mixins
  • variables
  • functions
  • exports

The following is an example config that would filter out all four declaration types:

customElementsManifestToMarkdown(manifest, {
  omitDeclarations: ['mixins', 'variables', 'functions', 'exports' ]
})

Note: Mixins can be rendered both as declarations AND as sections inside a declaration. The omitDeclarations option for mixins will only filter out top level mixin declarations. To filter out mixin sections from a class declaration, use the mixin filter from omitSections.

Omit Sections

The omitSections option is a string[] that controls which sections of a declaration's full entry in the manifest.json should be rendered in the final markdown output. The section names are:

  • mainHeading : "main-heading"
  • mainDescription : "main-description"
  • superClass : "super-class"
  • fields : "fields"
  • methods : "methods"
  • staticFields : "static-fields"
  • staticMethods : "static-methods"
  • slots : "slots"
  • events : "events"
  • attributes : "attributes"
  • cssProperties : "css-properties"
  • cssParts : "css-parts"
  • mixins : "main-heading"

The following is an example config showing how to filter out a few sections:

customElementsManifestToMarkdown(manifest, {
  // static fields and static methods tables will not be present
  // in the markdown result
  omitSections: [ 'super-class', 'static-methods', 'fields' ]
})

Class Name Filter

Depending on the source files you pass to the analyzer, your custom-elements-manifest.json may contain more class file declarations than you need for the final markdown output. The classNameFilter option accepts a regex as a string (or a function that returns one) that will be used to filter out class declarations before rendering.

customElementsManifestToMarkdown(manifest, {
  classNameFilter: () => {
    // some logic
    return `(${prefix}-*|SuperClassExact)`; // filters out every class name that doesnt match the regex provided
  }
})

Demo

customElementsManifestToMarkdown(manifest, {
  headingOffset: 1,
  private: 'details',
})
{
  "schemaVersion": "1.0.0",
  "readme": "",
  "modules": [
    {
      "kind": "javascript-module",
      "path": "./fixtures/-TEST/package/my-element.js",
      "declarations": [
        {
          "kind": "class",
          "description": "My description",
          "name": "SuperClass",
          "events": [
            {
              "name": "custom-event",
              "type": {
                "text": "SuperCustomEvent"
              },
              "description": "this is custom"
            }
          ],
          "superclass": {
            "name": "LitElement",
            "package": "lit-element"
          },
          "members": [
            {
              "kind": "method",
              "name": "superClassMethod",
              "privacy": "public"
            }
          ]
        },
        {
          "kind": "class",
          "name": "MyElement",
          "cssProperties": [
            {
              "name": "--background-color",
              "description": "Controls the color of bar"
            }
          ],
          "cssParts": [
            {
              "name": "bar",
              "description": "Styles the color of bar"
            }
          ],
          "slots": [
            {
              "name": "container",
              "description": "You can put some elements here"
            }
          ],
          "events": [
            {
              "name": "my-event",
              "type": {
                "text": "Event"
              }
            },
            {
              "name": "custom-event",
              "type": {
                "text": "SuperCustomEvent"
              },
              "description": "this is custom",
              "inheritedFrom": {
                "name": "SuperClass",
                "module": "./fixtures/-TEST/package/my-element.js"
              }
            }
          ],
          "mixins": [
            {
              "name": "LocalizeMixin",
              "package": "lion"
            },
            {
              "name": "Mixin",
              "module": "./fixtures/-TEST/package/my-element.js"
            }
          ],
          "superclass": {
            "name": "SuperClass",
            "module": "./fixtures/-TEST/package/my-element.js"
          },
          "attributes": [
            {
              "name": "prop-1",
              "fieldName": "prop1"
            },
            {
              "name": "prop2",
              "fieldName": "prop2"
            }
          ],
          "members": [
            {
              "kind": "field",
              "name": "prop1",
              "privacy": "public"
            },
            {
              "kind": "field",
              "name": "prop2",
              "privacy": "public"
            },
            {
              "kind": "field",
              "name": "prop3",
              "privacy": "public",
              "type": {
                "text": "boolean"
              },
              "default": "true"
            },
            {
              "kind": "field",
              "name": "foo",
              "type": {
                "text": "string"
              },
              "privacy": "private",
              "description": "description goes here",
              "default": "'bar'"
            },
            {
              "kind": "method",
              "name": "instanceMethod",
              "privacy": "public",
              "description": "Some description of the method here",
              "return": {
                "type": {
                  "text": ""
                }
              },
              "parameters": [
                {
                  "name": "e",
                  "type": {
                    "text": "Event"
                  }
                },
                {
                  "name": "a",
                  "type": {
                    "text": "string"
                  },
                  "description": "some description"
                }
              ]
            },
            {
              "kind": "field",
              "name": "mixinProp",
              "type": {
                "text": "number"
              },
              "privacy": "protected",
              "default": "1",
              "inheritedFrom": {
                "name": "Mixin",
                "module": "./fixtures/-TEST/package/my-element.js"
              }
            },
            {
              "kind": "method",
              "name": "superClassMethod",
              "privacy": "public",
              "inheritedFrom": {
                "name": "SuperClass",
                "module": "./fixtures/-TEST/package/my-element.js"
              }
            }
          ],
          "tagName": "my-element"
        },
        {
          "kind": "variable",
          "name": "variableExport",
          "description": "this is a var export",
          "type": {
            "text": "boolean"
          }
        },
        {
          "kind": "variable",
          "name": "stringVariableExport",
          "description": "this is a string var export",
          "type": {
            "text": "string"
          }
        },
        {
          "kind": "function",
          "name": "functionExport",
          "description": "This is a function export",
          "return": {
            "type": {
              "text": "boolean"
            }
          },
          "parameters": [
            {
              "name": "a",
              "type": {
                "text": "string"
              }
            },
            {
              "name": "b",
              "type": {
                "text": "boolean"
              }
            }
          ]
        },
        {
          "kind": "mixin",
          "name": "MyMixin4",
          "parameters": [
            {
              "name": "klass",
              "type": {
                "text": "*"
              },
              "description": "This is the description"
            },
            {
              "name": "foo",
              "type": {
                "text": "string"
              },
              "description": "Description goes here"
            }
          ]
        },
        {
          "kind": "mixin",
          "name": "Mixin",
          "parameters": [
            {
              "name": "klass",
              "type": {
                "text": "*"
              },
              "description": "This is the description"
            }
          ],
          "members": [
            {
              "kind": "field",
              "name": "mixinProp",
              "type": {
                "text": "number"
              },
              "privacy": "protected",
              "default": "1"
            }
          ]
        }
      ],
      "exports": [
        {
          "kind": "js",
          "name": "SuperClass",
          "declaration": {
            "name": "SuperClass",
            "module": "./fixtures/-TEST/package/my-element.js"
          }
        },
        {
          "kind": "custom-element-definition",
          "name": "my-element",
          "declaration": {
            "name": "MyElement",
            "module": "./fixtures/-TEST/package/my-element.js"
          }
        },
        {
          "kind": "js",
          "name": "variableExport",
          "declaration": {
            "name": "variableExport",
            "module": "./fixtures/-TEST/package/my-element.js"
          }
        },
        {
          "kind": "js",
          "name": "stringVariableExport",
          "declaration": {
            "name": "stringVariableExport",
            "module": "./fixtures/-TEST/package/my-element.js"
          }
        },
        {
          "kind": "js",
          "name": "functionExport",
          "declaration": {
            "name": "functionExport",
            "module": "./fixtures/-TEST/package/my-element.js"
          }
        }
      ]
    }
  ]
}

My description

./fixtures/-TEST/package/my-element.js:

class: SuperClass

Superclass

NameModulePackage
LitElementlit-element

Methods

NamePrivacyDescriptionParametersReturnInherited From
superClassMethodpublic

Events

NameTypeDescriptionInherited From
custom-eventSuperCustomEventthis is custom

class: MyElement, my-element

Superclass

NameModulePackage
SuperClass./fixtures/-TEST/package/my-element.js

Mixins

NameModulePackage
LocalizeMixinlion
Mixin./fixtures/-TEST/package/my-element.js

Fields

NamePrivacyTypeDefaultDescriptionInherited From
prop1public
prop2public
prop3publicbooleantrue

Methods

NamePrivacyDescriptionParametersReturnInherited From
instanceMethodpublicSome description of the method heree: Event, a: string
superClassMethodpublicSuperClass

Events

NameTypeDescriptionInherited From
my-eventEvent
custom-eventSuperCustomEventthis is customSuperClass

Attributes

NameFieldInherited From
prop-1prop1
prop2prop2

CSS Properties

NameDescription
--background-colorControls the color of bar

Slots

NameDescription
containerYou can put some elements here

Fields

NamePrivacyTypeDefaultDescriptionInherited From
fooprivatestring'bar'description goes here
mixinPropprotectednumber1Mixin

mixin: MyMixin4

Parameters

NameTypeDefaultDescription
klass*This is the description
foostringDescription goes here

mixin: Mixin

Parameters

NameTypeDefaultDescription
klass*This is the description

Fields

NamePrivacyTypeDefaultDescriptionInherited From
mixinPropprotectednumber1

Variables

NameDescriptionType
variableExportthis is a var exportboolean
stringVariableExportthis is a string var exportstring

Functions

NameDescriptionParametersReturn
functionExportThis is a function exporta: string, b: booleanboolean

Exports

KindNameDeclarationModulePackage
jsSuperClassSuperClass./fixtures/-TEST/package/my-element.js
custom-element-definitionmy-elementMyElement./fixtures/-TEST/package/my-element.js
jsvariableExportvariableExport./fixtures/-TEST/package/my-element.js
jsstringVariableExportstringVariableExport./fixtures/-TEST/package/my-element.js
jsfunctionExportfunctionExport./fixtures/-TEST/package/my-element.js