Skip to content

cedar-policy/highlightjs-cedar

highlight.js support for Cedar policy language and Cedar schema

highlight.js (syntax highlighting for the Web) support for Cedar based on Grammar specification for Cedar policy syntax and for Cedar schema based on Grammar specification for Cedar schemas. Project structure derived from language contributor checklist and mapped to the Stylable Scopes.

See it in action on https://docs.cedarpolicy.com/

Use

Web browser <script src=> usage example from test/static/index.html (adjust the script src paths as required):

    <script src="highlight.min.js"></script>
    <script src="hljs-cedar.min.js"></script>
    <script type="text/javascript">
      window.onload = () => {
        hljs.registerLanguage('cedar', hljsCedar);
        hljs.registerLanguage('cedarschema', hljsCedarschema);
        hljs.highlightAll();
      };
    </script>

ECMAScript module import usage example from test/vite/index.html (adjust the import from paths as required):

import hljs from 'highlight.js';
import { hljsCedar, hljsCedarschema } from 'hljs-cedar.mjs';
hljs.registerLanguage('cedar', hljsCedar);
hljs.registerLanguage('cedarschema', hljsCedarschema);
hljs.highlightAll();

Development

Install

Install the project dependencies using npm install.

src/cedar.js is the main source file in ECMAScript module format.

Build

The npm run build script uses esbuildto create multiple files in the dist folder:

  • hljs-cedar.js - Web browser <script src=> compatible version. Sets window.hljsCedar and window.hljsCedarschema to highlight functions.
  • hljs-cedar.min.js - Minified web browser <script src=> compatible version.
  • hljs-cedar.mjs - ECMAScript module version with default export. Used by test/cedar-esm.test.js test cases and test\vite\main.js from Vite web app.
    • import hljsCedar from '../dist/hljs-cedar.mjs';
  • hljs-cedar.cjs - CommonJS module version with default export. Used by test/cedar-commonjs.test.js test cases.
    • const hljsCedar = require('../dist/hljs-cedar.cjs').default;

Test

npm run test uses vitest to syntax highlight test/data/*.cedar and test/data/*.cedarschema files and compares against a generated .html File Snapshots for each Cedar and Cedar schema file.

View either the static test/static/index.html on your browser or npm run dev and test/vite/index.html renders in your browser: http://localhost:5173/

When new test/data/*.cedar or test/data/*.cedarschema files are created, npm run testdata will update the contents of each index.html file.

Pull Request

Pull requests require a Developer Certificate of Origin (DCO) to certify the right to submit the code they are contributing to the project. Either add the -s or the --signoff flag to your commits or update your .vscode/settings.json with git.alwaysSignOff.

{
  "git.alwaysSignOff": true
}

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.