npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typescript-docs-verifier

v2.5.0

Published

Verifies that typescript examples in markdown files actually compile.

Downloads

11,417

Readme

typescript-docs-verifier

Verifies that typescript examples in markdown files actually compile.

TypeScript JavaScript Style Guide Build Status Apache 2.0 TypeScript docs verifier

Why?

Ever copied a TypeScript code example from a README and found that it didn't even compile? This tool can help by verifying that all of your code examples compile correctly. And yes, the TypeScript code samples in this README are checked using this tool.

demo

Inspired the by the tut documentation compilation tool for scala.

How it works

The selected markdown files are searched for TypeScript code blocks marked like this:

```typescript
// Some TypeScript code here
const write = 'some code';
```

These code blocks are extracted and any imports from the current project are replaced with an import of the main or exports from package.json (e.g. import { compileSnippets } from 'typescript-docs-verifier' would be replaced with import { compileSnippets } from './dist/index' for this project).

Each code snippet is compiled (but not run) and any compilation errors are reported. Code snippets must compile independently from any other code snippets in the file.

The library can also be used to type check .tsx files:

```tsx
import React from 'react'

const SomeComponent = () => (
  <div>
    This is a TSX component!
  </div>
)
```

Ignoring code blocks

Individual code blocks can be ignored by preceding them with a <!-- ts-docs-verifier:ignore --> comment:

<!-- ts-docs-verifier:ignore -->
```typescript
// This block won't be compiled by typescript-docs-verifier
```

Script usage

node_modules/.bin/typescript-docs-verifier [--input-files <markdown-files-to-test>] [--project <path-to-tsconfig-file>]
  • --input-files is optional and defaults to README.md.
  • --project is optional and defaults to the tsconfig.json file in the package root.
  • Any compilation errors will be reported on the console.
  • The exit code is 1 if there are any compilation errors and 0 otherwise.

Library usage

TypeScript

import { compileSnippets, SnippetCompilationResult } from 'typescript-docs-verifier'
import * as http from 'http'

const markdownFiles = ['README', 'examples.md'] // defaults to 'README.md' if not provided
const tsconfigPath = 'docs-tsconfig.json' // defaults to the 'tsconfig.json' file in the package root
compileSnippets({ markdownFiles, project: tsconfigPath })
  .then((results: SnippetCompilationResult[]) => {
    results.forEach((result: SnippetCompilationResult) => {
      if (result.error) {
        console.log(`Error compiling example code block ${result.index} in file ${result.file}`)
        console.log(result.error.message)
        console.log('Original code:')
        console.log(result.snippet)
      }
    })
  })
  .catch((error: unknown) => {
    console.error('Error compiling TypeScript snippets', error)
  })

JavaScript

const { compileSnippets } = require('typescript-docs-verifier')

const markdownFiles = ['README.md', 'examples.md'] // defaults to 'README.md' if not provided
const tsconfigPath = 'docs-tsconfig.json' // defaults to the 'tsconfig.json' file in the package root
compileSnippets({ markdownFiles, project: tsconfigPath })
  .then((results) => {
    results.forEach((result) => {
      if (result.error) {
        console.log(`Error compiling example code block ${result.index} in file ${result.file}`)
        console.log(result.error.message)
        console.log('Original code:')
        console.log(result.snippet)
      }
    })
  })
  .catch((error) => {
    console.error('Error compiling TypeScript snippets', error)
  })

Development

Run the tests:

npm install
npm test

Contributing

See these notes for information for contributors.

License

typescript-docs-verifier is available to all via the Apache-2.0 license.

Copyright © 2017 BBC