Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@zaeny/literate

azizzaeny5GPL-3.01.0.8

Literate programming, extract code from markdown

literate, programming, extract, code, from, markdown

readme

@zaeny/literate

npm version npm downloads

literate programming, extract code blocks from markdown file

See related packages : @composable

Table of Contents

Usage

Node.js Installing

 npm install @zaeny/literate

Import

 import {extractCode} from 'https://cdn.jsdelivr.net/npm/@zaeny/literate';
 // import core code individually without side effects, and use in browser
 import {extractCode} from 'https://cdn.jsdelivr.net/npm/@zaeny/literate/src/core.js';

Extracting Code

var {extractCode}  = require('@zaeny/literate');
// or
var {extractCode} = await import('@zaeny/litarte');

var testMdFile = fs.readFileSync('./test.md', 'utf8');
extractCode(testMdFile);
/*
  [
    { path: 'index.js', lang: 'js', code: "console.log('welcome');" },
    { path: 'index.js', lang: 'js', code: "console.log('hai');" }
  ]
*/

evaluating markdown files

var {evaluateBlock} = require('@zaeny/literate');
// or
var {evaluateBlock} = await import('@zaeny/literate');

evaluateBlock(testMdFile);
/*
  welcome
  hai
*/
// more advanced,
// evaluateBlock(md, validation, context, requiredContext)
evalulateBlock(testMdFile, (file) => (file.code && file.lang === "js" && file.eval===1), global, {require, console, module });

extracting code blocks from markdown alias tangle

var {tangle} = require('@zaeny/literate');
var {tangle} = await import('@zaeny/literate');
tangle(testMdFile);
/*
  'tangle files: #1 index.js'
*/

tangle(testMdFile, (file) => (file.path && file.lang === "js"));
//  'tangle files: #1 index.js'

Documentation

check the index.md;

Changes

  • [1.0.2] tangle and eval now return object of inputed
  • [1.0.5] refactor groupBlockBy, rename eval to evalaute, move repository to composable
  • [1.0.7] add captureCode captureCodeAt
  • [1.0.8] fix homepage repository, add global to incldue in simple script tag literate