0.6.5 • Published 6 years ago

@boundless-inc/mobiledoc-dom-renderer v0.6.5

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

Mobiledoc DOM Renderer Build Status

This is a DOM renderer for the Mobiledoc format used by Mobiledoc-Kit.

To learn more about Mobiledoc cards and renderers, see the Mobiledoc Cards docs.

The renderer is a small library intended for use in browser clients.

Usage

var mobiledoc = {
  version: "0.3.0",
  markups: ["B"],
  atoms: [],
  cards: [],
  sections: [
    [1, 'P', [ // array of markers
      // marker
      [ 0,            // marker type 0 (standard text)
        [0],          // open markups (by index)
        0,            // close count
        'hello world'
      ]
    ]
  ]
};
var renderer = new MobiledocDOMRenderer({cards: []});
var rendered = renderer.render(mobiledoc);
var result = rendered.result;
document.getElementById('output').appendChild(result);
// renders <div><p><b>hello world</b></b></div>
// into 'output' element

The Renderer constructor accepts a single object with the following optional properties:

  • cards array - The list of card objects that the renderer may encounter in the mobiledoc
  • atoms array - The list of atom objects that the renderer may encounter in the mobiledoc
  • cardOptions object - Options to pass to cards and atoms when they are rendered
  • unknownCardHandler function - Will be called when any unknown card is enountered
  • unknownAtomHandler function - Will be called when any unknown atom is enountered
  • sectionElementRenderer object - A map of hooks for section element rendering.
    • Valid keys are P, H1, H2, H3, H4, H5, H6, BLOCKQUOTE, ASIDE
    • Arguments are tagName, dom
    • A valid value is a function that returns an element
  • markupElementRenderer object - A map of hooks for inline element rendering.
    • Valid keys are B, I, STRONG, EM, A, U, SUB, SUP, S, CODE
    • Arguments are tagName, dom, attributes={}
    • A valid value is a function that returns an element
  • dom object - A native or SimpleDOM implementation of the DOM.

The return value from renderer.render(mobiledoc) is an object with two properties:

  • result DOM Node - The rendered result
  • teardown function - When called, this function will tear down the rendered mobiledoc and call any teardown handlers that were registered by cards when they were rendered

Rendering HTML

In a browser, rendering to HTML is simple:

var renderer = new MobiledocDOMRenderer();
var rendered = renderer.render(mobiledoc);
var html = rendered.result.outerHTML;

However on the server in Node.js, native DOM APIs are not available. To make server-rendering easy, this DOM renderer is SimpleDOM compatible. You may pass an instance of a SimpleDOM document and serialize its output. For example:

var renderer = new MobiledocDOMRenderer({
  dom: new SimpleDOM.Document()
});
var rendered = renderer.render(mobiledoc);
var serializer = new SimpleDOM.HTMLSerializer([]);
var html = serializer.serializeChildren(rendered.result);

This usage of the DOM renderer for rendering HTML has the advantage of allowing developers to easily implement cards that work in a server and client context.

sectionElementRenderer

Use this renderer option to customize what element is used when rendering a section.

var renderer = new MobiledocDOMRenderer({
  sectionElementRenderer: {
    P: function(_, dom) { return dom.createElement('span'); },
    H1: function(_, dom) { return dom.createElement('h2'); },
    H2: function(tagName, dom) {
      var element = dom.createElement(tagName);
      element.setAttribute('class', 'subheadline');
      return element;
    }
    /* Valid keys are P, H1, H2, H3, H4, H5, H6, BLOCKQUOTE, ASIDE */
  }
});
var rendered = renderer.render(mobiledoc);

markupElementRenderer

Use this renderer option to customize what inline tags are used when rendering a section's content.

var renderer = new MobiledocDOMRenderer({
  markupElementRenderer: {
    B: function(_, dom) { return dom.createElement('strong'); },
    A: function(tagName, dom, attrs={}) {
      let element = dom.createElement(tagName);

      for (let attr in attrs) {
        element.setAttribute(attr, attrs[attr]);
      }

      element.setAttribute('rel', 'nofollow');
      return element;
    }
  }
});
var rendered = renderer.render(mobiledoc);

Attribute Sanitization (XSS Protection)

Mobiledoc DOM Renderer sanitizes the href attribute of 'A' markups, prefixing the string unsafe: on potentially unsafe urls. It determines an environment- appropriate URL protocol parser. In rare cases it may be unable to determine one (this can happen when running the renderer in a Node VM Sandbox, like ember-cli- fastboot does), and will throw in that case. To fix this, you can provide a custom markupElementRenderer for the 'A' tag that will be used instead of the default.

Tests

Releasing

  • Use np (install with npm install -g np)
  • np <version> (e.g. np 0.5.2)
  • git push --tags
add-streamafteracceptsacornamdefinealteransi-regexansi-stylesalign-textapplauseare-we-there-yetargparseaprobaJSONStreamarray-equalarray-ifyarray-flattenarray-find-indexast-typesasyncasync-disk-cachebabel-plugin-dead-code-eliminationbabel-corebabel-plugin-constant-foldingbabel-plugin-evalarraybuffer.slicebabel-plugin-jscriptbabel-plugin-inline-environment-variablesbabel-plugin-member-expression-literalsbabel-plugin-property-literalsbabel-plugin-proto-to-assignbabel-plugin-react-constant-elementsbabel-plugin-runtimebabel-plugin-remove-debuggerbabel-plugin-undeclared-variables-checkbabel-plugin-remove-consolebacko2babel-plugin-undefined-to-voidbabylonbackbonebase64idbinaryextensionsbase64-arraybufferbetter-assertbalanced-matchblank-objectbluebirdblobbreakablebroccoli-amd-loaderbroccoli-babel-transpilerbrace-expansionbroccoli-concatbroccoli-filterbroccoli-caching-writerbroccoli-jshintbroccoli-kitchen-sink-helpersbroccoli-pluginbroccoli-replacebroccoli-persistent-filterbroccoli-stewbuffer-shimsbroccoli-sourcemap-concatbroccoli-slow-treesbroccoli-writercamelcase-keysbuiltin-modulescallsitecamelcasecenter-alignchalkcliclonecliuican-symlinkcharmcode-point-atcompare-funccommandercommonercomponent-emittercomponent-inheritcomponent-bindconcat-mapconnectconsole-control-stringsconsole-browserifycontent-typeconsolidateconventional-changelog-codemirrorcontent-dispositionconventional-changelog-angularconventional-changelog-atomconventional-changelog-eslintconventional-changelog-emberconventional-changelog-coreconventional-changelog-jscsconventional-changelog-jqueryconventional-changelog-expressconventional-changelog-jshintconventional-changelog-writerconventional-commits-parserconvert-source-mapconventional-commits-filtercookie-signaturecookieast-traversecopy-dereferencecore-jscore-util-iscson-parsercross-spawncurrently-unhandleddate-nowdargsdebugdateformatdefineddecamelizedefsdelegatesdepddestroydetect-indentdomelementtypedom-serializerdetectivedomhandleree-firsteditionsdot-propdomutilsencodeurlengine.ioengine.io-parserentitiesensure-posix-pathengine.io-clientescape-string-regexpescape-htmlesprimaerror-exesprima-fbetagexitesutilsevents-to-arrayeventemitter3exists-syncexpressfast-ordered-setfast-sourcemap-concatfinalhandlerfind-upfirewormforwardedfindup-syncfollow-redirectsfs-extrafreshfs-readdir-recursivefs-tree-difffs.realpathgaugeget-stdinget-pkg-repogitconfiglocalgit-remote-origin-urlgit-raw-commitsgit-semver-tagsglobgraceful-readlinkgraceful-fsgithub-url-from-gitbabel-plugin-react-display-nameglobalshandlebarshas-colorgrowlyhas-ansihas-binaryhas-corshas-unicodehosted-git-infohome-or-tmphash-for-depheimdalljs-loggerhtmlparser2heimdalljshttp-errorsiconv-litehttp-proxyinflightindent-stringiniindexofinvert-kvinheritsis-arrayishis-bufferis-finiteipaddr.jsis-builtin-moduleis-subsetis-integeris-objis-typeis-utf8is-text-pathisarrayis-fullwidth-code-pointistextorbinaryisexejs-yamljs-tokensjsescjs-string-escapejson3jshintjson-stringify-safejson5json-stable-stringifyjsonfilekind-oflazy-cachejsonifyjsonparseload-json-fileloader.jslevenlcidlodash._baseflattenlodash._basecallbacklodashlodash-nodelodash._baseisequallodash._cacheindexoflodash._baseuniqlodash._baseindexoflodash._bindcallbacklodash._reinterpolatelodash._createcachelodash.assigninlodash._isiterateecalllodash._getnativelodash.findlodash.debouncelodash.flattenlodash.keyslodash.clonedeeplodash.isarraylodash.isargumentslodash.pairslodash.istypedarraylodash.templatelodash.templatesettingslongestloud-rejectionlodash.uniqlodash.uniqbylru-cachematcher-collectionmd5-hexmerge-descriptorsmd5-o-maticmeowmedia-typermap-objmimemethodsmime-dbminimistmktempminimatchmkdirpmodify-valuesmustachemime-typesmsncpnode-notifiernegotiatornumber-is-nannpmlognormalize-package-dataobject-assignonceobject-componenton-finishedos-localeos-tmpdiroutput-file-syncparse-github-repo-urloptimistoptionsparse-jsonparsejsonparseuriparseurlpath-is-absolutepath-posixparseqspath-typepinkie-promisepath-to-regexppifypinkieprintfprocess-nextick-argspath-existsproxy-addrpromise-map-seriespseudomapprivateqqsquick-temprange-parserread-pkg-upread-pkgqunitjsrecastredentreadable-streamregenerateregeneratorregjsparserregjsgenrepeat-stringregexpurepeatingrequires-portright-alignrimrafresolversvpsemversafe-bufferset-blockingsendsetprototypeofserve-staticshebang-commandshelljssigmundshebang-regexsimple-isshellwordssimple-fmtslashsocket.iosignal-exitsocket.io-clientsocket.io-adaptersource-map-supportsocket.io-parsersource-mapsource-map-urlspdx-correctspawn-argssplitspdx-expression-parsespdx-license-idssprintf-jssplit2stablestring_decoderstring-widthstringmapstatusesstrip-bomstrip-indentstringsetstrip-ansistrip-json-commentsstyled_stringsupports-colorsymlink-or-copytap-parsertext-extensionstempfiletextextensionsto-arraytmpthrough2throughto-fast-propertiestrim-off-newlinestrim-newlinestrim-righttry-resolvetryoruglify-to-browserifytype-isuglify-jsunderscoreunpipeuser-homeultronunderscore.stringutils-mergeuuidutil-deprecatevalidate-npm-package-licensewalk-syncvarywide-alignwindow-sizewhichwordwrapwswtf-8xmldomxmlhttprequest-sslwrappyyallisty18nxtendyeastyargs
0.6.5

6 years ago