GithubHelp home page GithubHelp logo

ekalinin / typogr.js Goto Github PK

View Code? Open in Web Editor NEW
297.0 14.0 20.0 131 KB

Typography utils for javascript

License: MIT License

Makefile 1.87% JavaScript 59.36% CSS 19.53% HTML 19.24%
nodejs typography smartypants javascript

typogr.js's Introduction

Build Status

Table of Contents

typogr.js

typogr.js provides a set of functions which automatically apply various transformations to plain text in order to yield typographically-improved HTML.

typogr.js is inspired by these awesome packages:

Installation

It's recommended to install via npm:

npm install -g typogr

Usage

typogr.js has no external dependencies and can be used both on the server and in the browser.

Simple on the server

// Only for server side
var typogr = require('typogr');

typogr.typogrify('<h1>"Pretty header ...</h1>');
'<h1><span class="dquo">&#8220;</span>Pretty header&nbsp;&#8230;</h1>'

Simple in the browser

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/ekalinin/[email protected]/typogr.min.js"></script>
<script>
$(document).ready(function() {
    $('#res').html(typogr.typogrify($('#src')));
})
</script>

OOP-style

// Only for server side
var typogr = require('typogr');

typogr('<h1>"Pretty header ...</h1>').typogrify();
'<h1><span class="dquo">&#8220;</span>Pretty header&nbsp;&#8230;</h1>'

Chains

// Only for server side
var typogr = require('typogr');

typogr('<h1>"Pretty header ...</h1>').chain().initQuotes().value();
'<h1><span class="dquo">"</span>Pretty header ...</h1>'

typogr('<h1>"Pretty header ...</h1>').chain().initQuotes().smartypants().value();
'<h1><span class="dquo">&#8220;</span>Pretty header &#8230;</h1>'

API

amp

Wraps ampersands in HTML with <span class="amp"> so they can be styled with CSS. Ampersands are also normalized to &amp;. Requires ampersands to have whitespace or an &nbsp; on both sides. Will not change any ampersand which has already been wrapped in this fashion.

initQuotes

Wraps initial quotes in <span class="dquo"> for double quotes or <span class="quo"> for single quotes. Works inside these block elements:

  • h1, h2, h3, h4, h5, h6
  • p
  • li
  • dt
  • dd

Also accounts for potential opening inline elements: a, em, strong, span, b, i.

smartypants

  • Straight quotes ( " and ' '") into “curly” quote HTML entities (‘ | ’ | “ | ”)
  • Backticks-style quotes (``like this''') into “curly” quote HTML entities (‘ | ’ | “ | ”)
  • Dashes (“--” and “---”) into n-dash and m-dash entities (– | —)
  • Three consecutive dots (“...”) into an ellipsis entity (…)

widont

Based on Shaun Inman's PHP utility of the same name, replaces the space between the last two words in a string with &nbsp; to avoid a final line of text with only one word.

Works inside these block elements:

  • h1, h2, h3, h4, h5, h6
  • p
  • li
  • dt
  • dd

Also accounts for potential closing inline elements: a, em, strong, span, b, i.

caps

Wraps multiple capital letters in <span class="caps"></span> so they can be styled.

ord

Wraps number suffix's in <span class="ord"></span> so they can be styled.

typogrify

Applies all of the following filters, in order:

  • amp
  • widont
  • smartypants
  • caps
  • initQuotes
  • ord

CLI

A command line interface can be used to typogrify html files.

% typogr --help

  Usage: typogr [options] [input] [output]

  Options:

    -h, --help     output usage information
    -V, --version  output the version number
    -i, --inplace  Use single path as both input and output
    -f, --force    Do not prompt to verify file overwrites

  reads input from stdin, individual files, directories, or globs
  writes ouput to stdout, individual files, or directories

  Examples:

    $ typogr inputFile.html outputFile.html
    $ typogr < inputFile.html > outputFile.html
    $ typogr -i singleFile.html
    $ typogr inputDirectory outputDirectory
    $ typogr inputDirectory/*.html outputDirectory

License

See LICENSE file.

typogr.js's People

Contributors

ekalinin avatar kennethormandy avatar brandonaaron avatar mattly avatar tlvince avatar josholdenburg avatar mnewt avatar worldmaker avatar andrey-p avatar bartveneman avatar matias-sandell avatar mheiber avatar lhagan avatar

Stargazers

 avatar Andrew Solanto avatar Github Notification avatar Stalin Kay avatar Jean-François Le Gac avatar Kai Devrim avatar Ed Galligan avatar Muhammad Ubaid Raza avatar Tenvi avatar Ted Klingenberg avatar Joseph Ribbe avatar Spas Z. Spasov avatar Elizabeth Yalkut avatar Vladislavs Judins avatar  avatar Eaton avatar Gil B.  avatar jump-and-jump avatar Fabien Franzen avatar Luis Guerrero avatar Franco Monsalvo avatar Peter Sampson avatar Martin Falk Johansson avatar Travis Spomer avatar Johhhn avatar Bogdan Slovyagin avatar Julien Verneaut avatar Daniel Haim avatar  avatar Maarten Idema avatar Nick avatar ChangJoo Park(박창주) avatar Cory LaViska avatar Chingun Erdene-Ochir avatar Sergey Kopylov avatar  avatar Junsoo avatar Free Art Bureau avatar ☟ avatar  avatar juytter avatar Mikhail Moukhin avatar Florian avatar  avatar Chris Amico avatar Wojtek Grojec avatar Cédric Aellen avatar Ian Rose avatar Ruben Del Rio avatar Carter avatar Jannick Scherf avatar  avatar Chris Biscardi avatar  avatar James Canning avatar  avatar Noah Lovelady-Allen avatar Raphael Mu avatar beedotnet avatar Robin de Mourat avatar Peter Sekan avatar Sam Baldwin avatar My Kim Bui avatar timur avatar Eric Hu avatar Nils Hörrmann avatar Ahmad Cecep Mughnillabib avatar Stefan Imhoff avatar Daniel Bayley avatar 巫书轶 avatar Chen Ye avatar Mark Feltner avatar Yaroslav avatar Dan Gayle avatar Jiří Matoušek avatar Richard Nii Lante Lawson avatar Swapnil Joshi avatar Lucas Dino Nolte avatar Daniel Gamage avatar Steve Edson avatar John Grishin avatar Kaho Cheung avatar Andy Graulund avatar Ryan de Gruyter avatar Adam Messinger avatar  avatar David A Roberts avatar Nick Marazzo avatar Bernardo Baquero Stand avatar Jan-T. Brinkmann avatar David Trejo avatar Kristle Chester avatar Jesse O'Chapo avatar  avatar Shoora avatar Coby Chapple avatar A. Bower avatar Jarryd van Zanten avatar Pete Petrash avatar Luís Rodrigues avatar

Watchers

 avatar  avatar  avatar Dave Crossland avatar timelyportfolio avatar James Cloos avatar Volkan Özçelik avatar Stewart Curry avatar  avatar Michael Anthony avatar Brian Koser avatar  avatar Richard Nii Lante Lawson avatar  avatar

typogr.js's Issues

handle rocknroll correctly

Should be rock 'n' roll instead of rock ‘n’ roll. (see Markdown source for what I mean by this)

That is, the 'n' should have two apostrophes instead of an opening and single quote and closing smart quote

Crashes when passed an empty string

I don't know if you would see it as a massive problem, but it just threw me off a bit. I'd expect passing an empty string to the typogrify method to return an empty string. Instead it throws the the following error:

    return text.replace(re_widont, '$1&nbsp;$2');
                ^
TypeError: Cannot call method 'replace' of undefined
    at typogr.widont (/home/andrey/Projects/writing/apocalism.js/node_modules/typogr/typogr.js:152:17)
    at Function.typogr.typogrify (/home/andrey/Projects/writing/apocalism.js/node_modules/typogr/typogr.js:228:12)

The problem is with the checks at the top of initQuotes amp and ord:

if( !text ) {
    return;
}

It should be solved by replacing the if statement with if (!text && typeof text !== string). Could send you a PR with a fix (+ tests) if you want.

Typogr auto-emdashes closing comment tags

Just noticed this on a client project. I’ve got typogr running on blocks of HTML. The double dashes in a closing comment tag (-->) are getting turned into an em dash.

Widon’t doesn’t count commas

If the second-last word in a paragraph has a comment before it, there will be no &nbps; after it.

Example

Start of the paragraph … before they get deleted—I mean, published.

Expected

<p>Start of the paragraph … before they get deleted—I mean,&nbsp;published.</p>

Actual

<p>Start of the paragraph … before they get deleted—I mean, published.</p>

PRE tags?

Is it possible to make it skip some tags? Like the "pre" or "abbr"?

apostrophe immediately after a tag is inverted

for example:

<a href="">markdown</a>'s popularity is growing

becomes:

<a href="">markdown</a>&#8216;s popularity is growing

which has the opposite smart-quote effect.

I'm not sure if this is the intended behavior or if other smartypants-style libraries do the same, but the results stick out pretty horribly to my eyes.

Support for other languages

Sadly, typography isn't easy. For example, in Germany we use „diese Anführungszeichen“ (these quotation marks). Other cultures, languages or styles use different ones as well. And it's probably not just about quotation marks.

Are there any plans to support more typography styles than just the English one?

Allow optional disabling of features when processing

I was thinking of submitting a pull request to provide the optional disabling of various features when run, but wanted to check with you first if you're be receptive to said PR. Typeset has to be ability to disable various features, based on an array of keywords—I was thinking to implement something similar for typogr.js.

Let me know what you think, thanks.

Matt

Work better with marked

This package doesn't work well with marked. Say I first run marked on a string of markdown, it will escape quotes to &#39; or &qout; which means typogr.smartypants won't typeset them. However if I first run typogr.smartypants on raw markdown it will change all underlined second headings to a series of em- and en-dashes, which the marked parser will subsequently miss.

I suggest we either leave lone dashes between newlines alone, or we typeset html encoded quotes along with ascii encoded ones (or even both so that the order of operation won't matter).

custom skips?

The markdown renderer I use puts some things I don't want to process in a <p class="mathjax"></p> tag, so it'd be nice to be able to specify something like p.mathjax as an ignore pattern, though it seems like you're actually doing this by parsing the html and not by using selectors, so maybe if we were able to add to the re_skip_tags pattern.

Smartypants inconsistent behavior in converting back tick style quotes to curly quotes

Smartypants converts back tick style quote in a different way if it's in the beginning of the string.

const typogr = require("typogr");

let text = "'wrong', 'right'";
text = typogr(text).chain().smartypants().value();

document.getElementById("app").innerHTML = text;

Result: &#8217;wrong&#8217;, &#8216;right&#8217;

Expected result: &#8216;wrong&#8217;, &#8216;right&#8217;

https://codesandbox.io/embed/mutable-water-7gmyi?fontsize=14&hidenavigation=1&theme=dark

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.