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

magebook

v1.2.19

Published

Interactive fiction editor

Downloads

7

Readme

Magebook API

API of the Magebook editor.

Magebook provides a command line tool and a javascript (Node.js) api to work with Magebook md format. You can use it to convert book formats, analyze books, or bundle your book inside a web app with Rollup or Vite.

Usage

To use the cli tool, install bun and run:

bunx magebook --help

Otherwise, install the api as a Node module using npm install magebook

Book indexes

The core feature of this api is an indexing function that generates a js object from a text, with every info about chapters, links and so on.

Book indexes have the following structure:

const book = {
  text: ... , // Here the full book text will be displayed
  index: {
    title: 'Book title',
    properties: {
      author: 'Luca Fabbian',
      revision: '1',
    },
    chapters: [
      {
        key: '1',
        title: '',
        group: 'groupname',
        flags: ['death'],
        lines: {
          start: 4,
          textStart: 5,
          textEnd: 8,
          end: 10,
        }
        links: ['1'], // keys of chapter a user could go from this one
        linkedFrom: [0], // index of chapters who are referring the key of this one
      }
    ],

    lineStarts: [ 0, 10, ...], // position of starting character of each line

    lines: {
      titlePageEnd: 3, // last line of the title page
      end: 10, // last line of the book
    }

    // default mapping for links
    keys: {
      '1': 0, // means that the chapter with index 0 is the default chapter with key 1
    },


    chaptersWith: {
      key: {
        '1': [0],
      }
      groups: {
        'groupname': [0],
      }
      flag: {
        'death': [0],
      }
    }
  },

  content: {
    titlePage,
    chapters: [
      {
        text: ... // full text of the chapter
        content: .... // just the content, purified by everything else
      }
    ]
  }
}

Thanks to book indexes, everything may be obtained in constant time without iterating the entire chapter array.

Some examples:

// get all the link of chapter with key 1

book.index.chapters[book.index.keys['1']].links

Encode/decode formats

Magebook is able to create a markdown text from other formats and then convert this text into html, docx, fodt, etc.

import {md, xlgc, fodt, docx, html, json } from 'magebook'


const test = `
# My Book
author: Luca Fabbian


### 1
Welcome`



// Or get the md file from another format
// const lgcBook = xlgc.decode(text)

// Write to fodt file
fs.writeFileSync('example.fodt', fodt.encode(data).encodedBook)

Rollup plugin

THIS FEATURE IS CURRENTLY UNDER REVISION To import a .md file inside a javascript code, you need a bundler. Magebook includes a rollup plugin already configured for that. It will read the book and transpile it to a standard format book (like above).

You can set the trasform option to encode markdown to html on the fly.

Example:

import { rollupMagebook, encodeChapter, textToHtml, htmlToText } from 'magebook';


// Set custom magebook parser
const magebookPlugin = rollupMagebook({
  transform: (key, chapter, book) => ({
    title: chapter.title,
    text: encodeChapter(chapter.text, {
      html:      text => textToHtml(text),
      paragraph: text => `${text}<br>`,
      strong:    text => `<b>${text}</b>`,
      em:        text => `<i>${text}</i>`,
      codespan:  text => htmlToText(text),
      code: (code)    => code,
      link: (href,i, text) => `<mage-link to="${href.replace('#', '')}">` +
          (text || book.chapters[href.replace('#', '')].title || href.replace('#', '')) +
        `</mage-link>`
    }),
  })
})


export default {
  // Put here your rollup config
  plugins: [
    magebookPlugin
  ]
}