Giter Site home page Giter Site logo

selector-state-frags's Introduction

Selector State Fragments

Converts between Web Annotation Selectors and States as Fragment Identifiers and Web Annotation Data Model Selectors and States (or SpecificResources containing those).

Install

npm install selector-state-frags

…or equivalent.

Use

Convert between fragment identifier and Selector or State object:

// for fragment identifiers of the form `selector(…)`
const { selector } = parse(fragmentIdentifier)

// for fragment identifiers of the form `state(…)`
const { state } = parse(fragmentIdentifier)

const fragmentIdentifier = stringify(selectorOrState)

A SpecificResource is simply treated as a pair of { source, selector }, or { source, state }. To convert between this object and a URI with the Selector/State as its fragment identifier:

const specificResource = uriToSpecificResource(uri)

const uri = specificResourceToUri(specificResource)

With uriToSpecificResource, if the given URI contains any other type of fragment identifier (neither selector nor state), it will be converted to its equivalent FragmentSelector:

uriToSpecificResource('https://example.com/page#section4')
// {
//     source: 'https://example.com/page',
//     selector: {
//         type: 'FragmentSelector',
//         value: 'section4',
//     }
// }

With specificResourceToUri, note that if the SpecificResource contains both a Selector and a State, only the Selector will be put in the fragment identifier.

Example

const selector = {
  "type": "TextPositionSelector",
  "start": 412,
  "end": 795
}
stringify(selector) === 'selector(type=TextPositionSelector,start=412,end=795)'
// And `parse('selector(…)')` would do the reverse conversion.

const specificResource = {
    "source": "http://example.org/ebook1",
    "selector": {
      "type": "TextPositionSelector",
      "start": 412,
      "end": 795
    }
}
specificResourceToUri(specificResource) === 'http://example.org/ebook1#selector(type=TextPositionSelector,start=412,end=795)'
// And uriToSpecificResource('http://…') would do the reverse conversion.

License

Apache License 2.0

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.