@zaeny/literate
v1.0.8
Published
Literate programming, extract code from markdown
Downloads
20
Maintainers
Readme
@zaeny/literate
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
andeval
now return object of inputed - [1.0.5] refactor
groupBlockBy
, renameeval
toevalaute
, 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