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

code-to-essay

v1.0.0

Published

The library essay is a code generator from a readme file but this library is a reverse operation.

Downloads

4

Readme

code-to-essay

The library essay is a code generator from a readme file but this library is a reverse operation.

Inspiration

I used eassy and made a simple project that can generate many code files of javascript (ES7). That can create a good document because you can update code and docs in the same time. I would like to create a converter from code files structure into readme. It will be also created by essay after convert them.

  • essay: https://github.com/dtinth/essay

Getting started

  1. Install this library
npm i -g code-to-essay
  1. Write a pathname with prefix c2e: in your README.md
c2e:src/main.js
  1. Convert them with command
c2e
  1. Setting your project follow essay format (https://github.com/dtinth/essay)

Now, the project could be generated by essay.

Implementation

main.js will start first when you call c2e build.

// main.js
#!/usr/bin/env node
import { getTextFromFile, writeFileFromText } from './file'
import { convertLinesToContexts } from './converter'
const README = getTextFromFile(`./README.md`)
const lines = README.split('\n')
const newContexts = convertLinesToContexts(lines)
const newDocs = newContexts.join('\n')
writeFileFromText(newDocs, './README.md')

converter.js is a module that contains

  • isCode check this line is start with c2e: that mean This line is code, right ?
  • convertCodeToContext convert code c2e: to context (code and block)
  • convertLineToContext pass line as params and return the correct context
  • convertLinesToContexts convert all lines to contexts (array)
// converter.js
import { getTextFromFile, writeFileFromText } from './file.js'

const isCode = (line) => {
  return line.indexOf('c2e:') === 0
}

const convertCodeToContext = (line) => {
  const pathname = line.split(':').pop()
  const header = `// ${pathname}`
  const code = getTextFromFile(pathname)
  const end = "\`\`\`"
  const start = end + 'js'
  const context = [start, header, code, end].join('\n')
  return context
}

const convertLineToContext = (line) => {
  return isCode(line) ? convertCodeToContext(line) : line
}

export const convertLinesToContexts = (lines) => {
  const contexts = lines.map((line) => {
    const context = convertLineToContext(line)
    return context
  })
  return contexts
}

file.js is a module that contains

  • getTextFromFile receive pathname and return text
  • writeFileFromText receive text and pathname as params and write file
// file.js
import { resolve, join } from 'path'
import { readFileSync, writeFileSync } from 'fs'

const projectPath = resolve()

export const getTextFromFile = (pathname) => {
  console.log("%s %s", "Reading", pathname)
  return readFileSync(join(projectPath, pathname), 'utf-8')
}

export const writeFileFromText = (text, pathname) => {
  console.log("%s %s", "Writing", pathname)
  return writeFileSync(join(projectPath, pathname), text, 'utf-8')
}