Giter Site home page Giter Site logo

emphasis's Introduction

Emphasis

Now with 100% less jQuery.

Emphasis provides dynamic paragraph-specific anchor links and the ability to highlight text in a document, all of which is made available in the URL hash so it can be emailed, bookmarked, or shared.

For more information and examples please go to this blog post:

http://open.blogs.nytimes.com/2011/01/11/emphasis-update-and-source/

Configuration

The main configuration element is specifying what paragraph elements are in scope and are not. To this end we specify the elements on or near Line 54:

this.paraSelctors = document.querySelectorAll('#article p');

This covers a lot of common markup in many sites and blog. However this could be configured for your specific site.

Example: If all your P tags reside in DIV tags with the "entry" classname, then this would be sufficient:

this.paraSelctors = document.querySelectorAll(".entry p");

Over at The New York Times, we'd use the following:

this.paraSelctors = document.querySelectorAll('#story-body > p');

Usage

Once up and running, a reader can double tap the SHIFT key to show the Paragraph links. Once in this mode they can toggle links and highlighting on a sentence level

Thanks

Levenshtein calculation in the script is based on some nice code by Andrew Hedges http://andrew.hedges.name/experiments/levenshtein/

To-Do

  • Further work on UI for highlighting with focus on simplicity
  • Social
  • Support for touch-based devices

emphasis's People

Contributors

benbalter avatar dgw avatar donohoe avatar lirenzhu avatar robflaherty avatar spencersugarman avatar vrish88 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

emphasis's Issues

gh-pages branch

Could you please create a gh-pages branch so the demo can be viewed here at Github? I did it in my fork and it lets people play with the demo right away, which is nice. Instructions on how to do it are here.

Unfortunately I don't think pull requests can create branches, otherwise I'd submit one...

Enabling emphasis for mobile devices

I am working on integrating emphasis for use with iPhones and iPads. I can not seem to activate emphasis from my iPad.

Do I need to implement another event that will work on mobile devices? Has anyone already done this?

IE doesn't like st.innerHTML

IE doesn't running addCSS() starting at line 67 in emphasis-src.js, and the reason is st.innerHTML. If you try opening any article on NYTimes (and any subsequent websites that have implemented this version of Emphasis) in IE, you will see an "Unknown runtime error."

I've recently added a jQuery fork of Emphasis onto my own site and ran into the same problem. Here's the fix I employed:

addCSS: function() {
/*  Inject the minimum styles rules required */
    var st = document.createElement('style');
    // for xhtml validation goodness
    st.setAttribute('type', 'text/css');

    // placed all the css in a variable
    var stStr = 'p.' + this.classActive + ' span { background-color:#f2f4f5; } p span.' + this.classHighlight + ' { background-color:#fff0b3; } span.' + this.classInfo + ' { position:absolute; margin:-1px 0px 0px -8px; padding:0; font-size:10px; background-color: transparent !important} span.' + this.classInfo + ' a { text-decoration: none; } a.' + this.classActiveAnchor + ' { color: #000; font-size: 11px; }';

    // try it the current way, if it fails (and it will in IE) do it the way that works in IE.
    try {
      st.innerHTML = stStr;
    } catch(e) {
      st.styleSheet.cssText = stStr;
    }

    document.getElementsByTagName("head")[0].appendChild(st);
},

Quoted text causes highlighting to become greedy

If you click to select a sentence that contains a quote, and that quote terminates the sentence, the highlighting process doesn't stop until it hits the end of a following "plain" sentence.

This case doesn't exist in the demo document, so we have to construct it.

Steps:

  • Take the current first paragraph ("The morning after…") and tack it onto the end of the second ("We are not so unlucky"…), combining them into a new single paragraph.
  • Activate Emphasis.
  • Select the new first paragraph.
  • Select the second sentence, beginning "When I remember…"
  • The highlight will blow past the quote ending that sentence, including the next one in the highlight, at which point it stops:

highlight-1

If we happen to have consecutive terminal quotes, the process repeats until a "plain" sentence is reached. Rearranging the demo document again:

highlight-2

Google Chrome

Just a heads-up...

Tested this against FF and Chrome. Works in FF, but double shift, paragraph selection and URL building don't seem to work in Chrome. Also, Chrome takes a built url and highlights the appropriate sentence, but does not seem to scroll/focus to the highlighted area.

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.