Coder Social home page Coder Social logo

prism-element's Introduction

Published on NPM Build status Published on webcomponents.org

<prism-highlighter>

Syntax highlighting via Prism.

Place a <prism-highlighter> in your document, preferably as a direct child of <body>. It will listen for syntax-highlight events on its parent element, and annotate the code being provided via that event.

The syntax-highlight event's detail is expected to have a code property containing the source to highlight. The event detail can optionally contain a lang property, containing a string like "html", "js", etc.

This flow is supported by <marked-element>.

See: Documentation, Demo.

Usage

Installation

npm install --save @polymer/prism-element

In an html file

<html>
  <body>
    <div id="parent">
      <prism-highlighter></prism-highlighter>
      <div id="output"></div>
    </div>
  </body>
  <script type="module">
      import '@polymer/prism-element/prism-highlighter.js';
      import '@polymer/prism-element/prism-theme-default.js';
      import '@polymer/polymer/lib/elements/custom-style.js';
      import {html} from '@polymer/polymer/lib/utils/html-tag.js';

      // import prism theme styles
      const template = html`
        <custom-style>
          <style include="prism-theme-default"></style>
        </custom-style>
      `;

      document.head.appendChild(template.contents);

      const parent = document.getElementById('parent');
      const output = document.getElementById('output');
      const ev = new CustomEvent('syntax-highlight');

      // enter code and language here
      ev.detail = {
        code: 'p { background-color: blue }',
        lang: 'css',
      };
      parent.dispatchEvent(ev);

      // detail is modified by prism-highlighter
      output.innerHTML = ev.detail.code;
    </script>
</html>

In a Polymer 3 element

import {PolymerElement, html} from '@polymer/polymer';
import '@polymer/prism-element/prism-element.js';
import '@polymer/prism-element/prism-highlighter.js';
import '@polymer/prism-element/prism-theme-default.js';

class SampleElement extends PolymerElement {
  static get template() {
    return html`
      <style include="prism-theme-default"></style>
      <div id="parent">
        <prism-highlighter></prism-highlighter>
        <div id="output"></div>
      </div>
    `;
  }

  connectedCallback() {
    super.connectedCallback();
    const ev = new CustomEvent('syntax-highlight');

    // enter code and language here
    ev.detail = {
      code: 'p { background-color: blue }',
      lang: 'css',
    };
    this.$.parent.dispatchEvent(ev);

    // detail is modified by prism-highlighter
    this.$.output.innerHTML = ev.detail.code;
  }
}
customElements.define('sample-element', SampleElement);

Contributing

If you want to send a PR to this element, here are the instructions for running the tests and demo locally:

Installation

git clone https://github.com/PolymerElements/prism-element
cd prism-element
npm install
npm install -g polymer-cli

Running the demo locally

polymer serve --npm
open http://127.0.0.1:<port>/demo/

Running the tests

polymer test --npm

prism-element's People

Contributors

43081j avatar aomarks avatar atotic avatar bicknellr avatar dfreedm avatar e111077 avatar ebidel avatar fredkschott avatar justinfagnani avatar nevir avatar notwaldorf avatar rictic avatar tedium-bot avatar timvdlippe avatar usergenic avatar valdrinkoshi avatar wesalvaro avatar

Stargazers

 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

prism-element's Issues

Double Highlight

This element is designed to be a "drop in once on the page" affair, but sometimes that's not going to be possible (for example, when a third-party element makes use of it that you're including on your page). Right now, this can cause a double-highlight where the highlight HTML is then highlighted.

I'd propose some kind of mechanism (shouldn't be too hard) that marks a highlight as already having happened and therefore not allowing other listening prism-highlighter elements to double up.

Not working for Polymer 3

Followed the Documentation to use this in a Polymer 3 element but it doesn't work.

Description

Following error is shown in Chrome's Console.
my-view1.js:179 Uncaught TypeError: Cannot assign to read only property 'detail' of object '#'
at HTMLElement.connectedCallback (my-view1.js:179)
at my-view1.js:190

Basically, in the connectedCallback() {
super.connectedCallback();
var ev = new CustomEvent('syntax-highlight'); // enter code and language here
ev.detail = {....};

The error suggests that ev.detail is readOnly.
Also, there is another error that below import is not found.
import '@polymer/prism-element/prism-element.js';

Expected outcome

Actual outcome

Live Demo

Steps to reproduce

Browsers Affected

  • Chrome
  • Firefox
  • Safari 9
  • Safari 8
  • Safari 7
  • Edge
  • IE 11
  • IE 10

html code send to prism-highlighter is parsed by polymer

Description

code send to prism-highlighter element is actually 'parsed' by polymer.
used prism-demo as example
https://github.com/PolymerElements/prism-element/blob/master/demo/prism-demo.html
<dom-module id="my-element">
<template>
console.log('hello world")
</template>
</dom-module>
<my-element></my-element>
<prism-demo code="<my-element></my-element>" lang="html"></prism-demo>

Expected outcome

hello world

Actual outcome

hello world
hello world

Browsers Affected

  • Chrome
  • Firefox
  • [?] Safari 9
  • [?] Safari 8
  • [?] Safari 7
  • Edge
  • [?] IE 11
  • [?] IE 10

Component not working while in shadowdom mode

Description

Syntax highlight doesnt work if Polymer is configured to run under shadow dom mode

Expected outcome

Highlighting must work

Actual outcome

No code highlight. But the code gets rendered.

Steps to reproduce

  <script>
    /* this script must run before Polymer is imported */
    window.Polymer = {
      dom: 'shadow',
      lazyRegister: true
    };
  </script>

Browsers Affected

  • Chrome
  • Firefox
  • Safari 9
  • Safari 8
  • Safari 7
  • Edge
  • IE 11
  • IE 10

Event model is flaky

This is kind of a meta-bug, related to issues reported on the catalog (googlearchive/polymer-element-catalog#207) and elsewhere.

Briefly, the ordering of attached can vary based on whether you're polyfilled or not, so the current interaction model for this element is racy.

I think this was an early experiment in loosely-coupled components, but the protocol isn't reliable.

I suggest that we could either make it more reliable, or simply turn prism-element into a prism-behavior that exposes a single highlight(code, language-hint) function. Which would remove the loose coupling benefits, but be fast and reliable.

... Or we tell users they should fire the event, and see if they get results, and if not, wait a rAF and try again.

Suggest proper name is `prism-element`

The Molecules product is aimed at containing elements that wrap third-party libraries. The current naming convention for such elements is <name-of-lib>-element.

If I've missed some context, my bad.

Demo?

Any plans to include a working demo for this one? Just having issues getting it working out of the box - possibly because it's nested too deeply - but having an example would help diagnose.

Highlighting not working when inside a dom-repeat template

Description

Highlighting doesn't work when it is inside a dom-repeat template. Reason being the _highlight method inside prism-highlighter.html never gets called. It will be nicer if we expose the prism API directly as a method on the component. It works if I explicitly use the _highlight method.

Expected outcome

Code highlights

Actual outcome

The tag not getting rendered

Live Demo

https://jsbin.com/vipuxo/2/edit?html,output

Steps to reproduce

Have a prism-element inside a dom-repeat template

Browsers Affected

  • Chrome
  • Firefox
  • Safari 9
  • Safari 8
  • Safari 7
  • Edge
  • IE 11
  • IE 10

Highlighting not working with V2

Description

Code highlighting isn't working with v2 preview. The fixed space font is correct, but that's it.

Expected outcome

Code highlighting should work as for v1.

Actual outcome

There is no code highlighting.

Steps to reproduce

The standard sample code doesn't highlight the html.

Browsers Affected

  • Chrome
  • Firefox
  • Safari 9
  • Safari 8
  • Safari 7
  • Edge
  • IE 11
  • IE 10

Demo Missing

Description

When you click on the "Demo" link in the component's page in the Polymer Elements catalog, an error message appears telling you "There isn't an element for that yet. It seems like you may have misspelled that URL...Take me to the front page".

Expected outcome

The demo for the component should appear.

Typo in prism-highlighter.html

There is a typo in prism-highlighter.html, line 85.
Line is:

return Prism.langauges.clike;

Should be:

return Prism.languages.clike;

Can be used as an editor for markdown ?

Description

Expected outcome

Actual outcome

Live Demo

Steps to reproduce

Browsers Affected

  • Chrome
  • Firefox
  • Safari 9
  • Safari 8
  • Safari 7
  • Edge
  • IE 11
  • IE 10

Just wondering if it can be used as an online editor.
I only need to edit markdown documents.

I need to add image upload download also but that would be s different component

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.