Giter Site home page Giter Site logo

github-slugger's Introduction

github-slugger

npm Build

Generate a slug just like GitHub does for markdown headings. It also ensures slugs are unique in the same way GitHub does it. The overall goal of this package is to emulate the way GitHub handles generating markdown heading anchors as close as possible.

This project is not a markdown or HTML parser: passing alpha *bravo* charlie or alpha <em>bravo</em> charlie doesn’t work. Instead pass the plain text value of the heading: alpha bravo charlie.

Install

npm install github-slugger

Usage

import GithubSlugger from 'github-slugger'

const slugger = new GithubSlugger()

slugger.slug('foo')
// returns 'foo'

slugger.slug('foo')
// returns 'foo-1'

slugger.slug('bar')
// returns 'bar'

slugger.slug('foo')
// returns 'foo-2'

slugger.slug('Привет non-latin 你好')
// returns 'привет-non-latin-你好'

slugger.slug('😄 emoji')
// returns '-emoji'

slugger.reset()

slugger.slug('foo')
// returns 'foo'

Check test/fixtures.json for more examples.

If you need, you can also use the underlying implementation which does not keep track of the previously slugged strings (not recommended):

import GithubSlugger, {slug} from 'github-slugger'

slug('foo bar baz')
// returns 'foo-bar-baz'

slug('foo bar baz')
// returns the same slug 'foo-bar-baz' because it does not keep track

Contributing

Contributions welcome! Please read the contributing guidelines first.

License

ISC

github-slugger's People

Contributors

bobylito avatar flet avatar mdjermanovic avatar mhkeller avatar parthpp avatar revin avatar styfle avatar susisu avatar tadatuta avatar uzitech avatar wooorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

github-slugger's Issues

Support for german umlaute

German Umlaute characters like 'ö', 'ä', 'ü' should be converted into 'oe', 'ae' or 'ue' to have less problems with different browser support.

Accented letters

Hi,

Accented letters do not seem to be converted to their "unaccented" counterpart: Prétérit gives prétérit instead of preterit.

I reckon something identical happens with the apostrophe: L'aoriste gives laoriste which is imperfect, l-aoriste would be better (for both readability and SEO purposes I guess), or worst case aoriste (dropping the l completely, if need be).

What do you think?

Best,

Lozenge ◊ not stripped?

It's my own fault for making headings using the lozenge character (◊, U+25CA), but

var GithubSlugger = require("github-slugger")
var slugger = new GithubSlugger();
console.log(slugger.slug('◊sent'))

prints ◊sent instead of sent like GitHub does.

Bug with Unicode

> S = require('github-slugger');
[Function: BananaSlug]
> s = new S
BananaSlug { occurrences: {} }
> s.slug('I ♥ unicode')
'i-♥-unicode'

...but, GitHub slugs that to i--unicode.

Apparently, this used to work fine: github-slugger used to output i--unicode (compare the last Travis build with the one I just triggered).

It’s rather weird that this changed, I’ve got no idea why...

id collision with number in text

var GithubSlugger = require('github-slugger')
var slugger = new GithubSlugger()

slugger.slug('foo')
// returns 'foo'

slugger.slug('foo')
// returns 'foo-1'

slugger.slug('foo 1')
// returns 'foo-1'

This outputs two of the same slugs

Postgres function

Is there anyway to mimic a postgres slugify function for this? I use this slugger on the front-end, but I also want my database to manually created the slugs for updated/inserted data. Ive been trying to make a working copy of this but every resource I have found online usually does not include multiple spaces as dashes and just trims it.

Breaking changes in 1.1

Hello! Greenkeeper just notified me that github-slugger 1.1 broke npm/marky-markdown; the issue is that in 1.0, unicode emoji characters in headings were being stripped out, but now they're being converted to HTML entities.

For example, given ## 😄-😄 unicode hyphen unicode:

  • 1.0 rendered --unicode-hyphen-unicode
  • 1.1 now renders &#x1F604;-&#x1F604;-unicode-hyphen-unicode

...which has broken a handful of our tests. Is there a way to get the old behavior? Thanks! 👍

Expose slugger function

Thanks for this project. In a project of mine, I need the slug function, but the iteration gets in the way. I want to expose the slugger function as a "static" method of the GithubSLugger object. This change would let us use the slugger directly, in this way:

import GithubSlugger from 'GitHub-slugger'

GithubSlugger.slugger('my text to slug')

What do you think?

Slugger for Foliant CustomIDs

I want to make a parser for headings in Foliant projects with CustomIDs.

For example # Long heading {#shortref} should result in two refs:

  • long-heading
  • shortref

Your projects has a basic functionality for this task. I tried to add a possibility to parse CustomdIDs, but it became incompatible with existing version because slugger function returns an array of strings now.

Do you need such functionality in this project or I just can continue my efforts in the fork?

Accents/diacritics should be replaced

Currently I have headings with accents that cause some trouble when linking from another page. For example:

var GithubSlugger = require("github-slugger")
var slugger = new GithubSlugger()
console.log(slugger.slug('Ñacurutú'))

Prints ñacurutú and it should print -acurut-.

I could fix this, if you are interested.

html tags should be removed

Html tags should be removed entirely not just the brackets.

example

slugger.slug('header <em>html</em>')

expected

header-html

actual

header-emhtmlem

Foreign characters not converted properly

e.g. Comment démarrer et configurer le transfert d'e-mails becomes #comment-d%C3%A9marrer-et-configurer-le-transfert-de-mails, which is an unrecognized expression

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.