Code Monkey home page Code Monkey logo

permalink's Introduction

permalink.js

Creates permalinks on existing sites and jumps to them.

Automatic permalinks for blog posts

There's been so many times that I've wished I could quickly link to a specific headline in an article, but there's no id attributes on the tag...so I can't.

But then there's nothing I can do to fix it, because it actually requires the blog author to overhaul their site to add ids to all the heading tags which may not be a quick job.

So, my dear blog author, here's my work around until you do serve ids in your headings.

JavaScript to generated permalinks

We're going to add a single JavaScript file that will crawl through all our heading elements, and convert the text of the heading into an id.

Then, if there's a hash fragment in the URL, the window will jump down to the named element. Thus "faking" permalink support.

The id

The id attribute is only applied if there's no id on the heading element and is simply a regular expression that strips away anything that isn't an alphanumeric and converts it to lowercase.

So a heading that reads: "What does a Pro account get you?" is converted to whatdoesaproaccountgetyou. Pretty simple.

Exposing the link

Inspired by Github's readme permalinks, when you hover near the left of the title then you'll see a ΒΆ character that is clickable and gives the reader the permalink.

This style is injected via the JavaScript, and it's quite possible you'll need to tweak it a little to get it style correctly on your own site (but it's pretty small).

Usage

Simple. Just slap the script in the footer of your site (or after all your heading tags) and it'll upgrade your headings for your visitors:

<script src="js/permalink.js"></script>

Now I can share headline specific links to your site whilst all you needed to do was add one line. Thank you!

permalink's People

Contributors

patik avatar remy avatar

Watchers

 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.