Coder Social home page Coder Social logo

ehne / microtext Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 524 KB

a library of small (and probably inefficient) utility functions for messing with text.

Home Page: https://www.npmjs.com/package/microtext

License: MIT License

JavaScript 100.00%
js string-manipulation markdown-parser markdown template-language

microtext's Introduction

logo

microtext

A library of small (and probably inefficient) utility functions for messing with text.

Getting started

node:

$ npm i microtext

and in your js:

const { micromarkup, microtemplate } = require('microtext')

or if you prefer ES Modules (browser):

import { microtemplate, micromarkup } from 'https://cdn.skypack.dev/microtext';

Functions exported:


micromarkup

A fairly small "markdown-light" parser.

Takes input of a string and returns the appropriate html.

Syntax

  • Links — default
    • The exact same as normal markdown:
    • [hello](# )<a href="#" target="_blank" rel="noopener noreferrer">hello</a>
  • Italics — default
    • Once again the same as normal markdown:
    • *hello*<i>hello</i>
    • micromarkup also supports the underscore syntax for italics: _hello_
  • Bold — default
    • **hello**<b>hello</b>
    • micromarkup also supports the double underscore syntax for bold: __hello__
  • Highlights — optional
    • ::hello::<mark>hello</mark>
  • Superscript — optional
    • ˆhelloˆ<sup>hello</sup>
    • to type ˆ on a mac: option shift I
  • Subscript — optional
    • ˇhelloˇ<sub>hello</sub>
    • to type ˇ on a mac: option shift T

note: micromarkup does not parse paragraphs into <p> tags.

Usage

//// BASIC
micromarkup(inputText)
// eg:
micromarkup("**bold text!**")
// returns: <b>bold text!</b>
  
//// WITH OPTIONS
micromarkup(inputText, options)
// eg:
micromarkup("::highlighted text!::", { highlighted: true })
// returns: <mark>highlighted text!</mark>

where:

  • inputText is a string of micromarkup text formatted with the syntax above (required)
  • options is a key-value object that contains the options defined below (optional)

Options

default = {
    links: true,
    newTab: true,
    bold: true,
    italics: true,
    highlights: false,
    supSub: false
  }
  • links: specifies whether or not links should be parsed.
  • newTab: whether or not the link parser should make links open in a new tab.
  • bold: whether or not bold text should be parsed.
  • italics: whether or not italic text should be parsed.
  • highlights: whether or not highlights should be parsed.
  • supSub: whether or not superscripts and subscripts should be parsed.

microtemplate

A simple template language that just inserts variables into your text.

Takes input of a string and returns another string with variables subbed in.

Syntax

hello λworldVarλ

Anything inside of the λ symbols will be treated as the variable name to replace with the text you want.

Usage

//// BASIC
microtemplate(inputString, variables)
// eg.
microtemplate('hello λvarλ', {'var': 'world'})
// returns: 'hello world'
  
//// WITH CUSTOM DELIMITER
microtemplate(inputString, variables, delimiter)
// eg.
microtemplate('hello §var§', {'var': 'world'}, '§')
// returns: 'hello world'

where:

  • inputString is the input text formatted with the syntax above. (required)
  • variables is an object of key-value pairs, where the key is the text inside the delimiter (λ), and the value is what to replace it with. (required)
  • delimiter is the symbol that is used to contain the variable names. The default is the λ symbol. (optional)

microdata

a very light data storage language, only really designed for static data.

Takes a string input and returns a JS object.

Syntax

[hello]
world

The above text returns the js:

{
  hello: "world",
}

note that microdata removes newlines from the original string for its very simple algorithm to work.

Usage

//// BASIC
// eg.
microdata(`
[hello]
cool
`) 
/* returns:
{
  hello: "cool"
}

Made by @ehne. Consider giving microtext a ⭐️ if it helped you!

npm GitHub Repo stars

microtext's People

Contributors

ehne avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.