├── .github
└── workflows
│ └── npm-publish.yml
├── .gitignore
├── LICENSE
├── README.md
├── dist
└── svelte.min.js
├── package-lock.json
├── package.json
├── rollup.config.js
├── src
├── browser.js
├── index.js
└── svelte.js
└── tests
├── 1-simple-component.svelte
├── 2-pngwn-example.svelte
├── 3-most-common-component.svelte
├── 4-js-testing.svelte
├── helpers
├── browser.js
├── snapshots.js
├── testing.js
└── visual.js
├── snapshots
├── 1-simple-component.html
├── 2-pngwn-example.html
├── 3-most-common-component.html
└── 4-js-testing.html
└── test.js
/.github/workflows/npm-publish.yml:
--------------------------------------------------------------------------------
1 | name: Publish on NPM
2 |
3 | on:
4 | push:
5 | paths:
6 | - '.github/workflows/npm-publish.yml'
7 | - 'package.json'
8 |
9 | jobs:
10 | test:
11 | runs-on: ubuntu-18.04
12 | steps:
13 | - uses: actions/checkout@v1
14 | - name: Setup Node
15 | uses: actions/setup-node@v1
16 | with:
17 | node-version: 12
18 | - name: Installing NPM deps
19 | run: npm install
20 | - name: Testing
21 | run: npm run test
22 |
23 | publish-npm:
24 | needs: test
25 | runs-on: ubuntu-18.04
26 | steps:
27 | - uses: actions/checkout@v1
28 | - name: Setup Node
29 | uses: actions/setup-node@v1
30 | with:
31 | node-version: 12
32 | registry-url: https://registry.npmjs.org/
33 | - name: Installing NPM deps
34 | run: npm install
35 | - name: Building & publishing on NPM
36 | run: npm publish
37 | env:
38 | NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | dist/index.*
2 | node_modules
3 | bundle*
4 | tests/visual
5 | tests/browser
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Alexey Schebelev
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # highlightjs-svelte
2 | Svelte language definition for Highlight.js
3 |
4 | ## install
5 | ```bash
6 | npm i highlight.js highlightjs-svelte
7 | ```
8 |
9 | ## usage
10 |
11 | ### browser
12 |
13 | ```html
14 |
15 |
16 |
19 |
Hello, {name}!
20 | ```
21 |
22 | ### node
23 | ```javascript
24 | const hljs = require('highlight.js');
25 | const hljs_svelte = require('highlightjs-svelte');
26 |
27 | hljs_svelte(hljs);
28 |
29 | const highlighted = hljs.highlight('svelte', source).value;
30 | ```
31 |
32 |
--------------------------------------------------------------------------------
/dist/svelte.min.js:
--------------------------------------------------------------------------------
1 | !function(){"use strict";hljs.registerLanguage("svelte",(function(e){return{subLanguage:"xml",contains:[e.COMMENT("\x3c!--","--\x3e",{relevance:10}),{begin:/^(\s*)(
4 |
5 | Hello, {name}!
6 |
7 |
--------------------------------------------------------------------------------
/tests/2-pngwn-example.svelte:
--------------------------------------------------------------------------------
1 | {#each array.map(({a, b, c}) => ({ a: a+1, b, c})) as {a, b, c} }
2 | {name === undefined ? 'Anybody' : name}
3 | {/each}
--------------------------------------------------------------------------------
/tests/3-most-common-component.svelte:
--------------------------------------------------------------------------------
1 |
6 |
7 | Hello, {name}!
8 | {#each list as item}
9 | { typeof item === 'str`===ing?' ? item.toUpperCase() : item}
10 | {:else}
11 | No items...
12 | {/each}
13 |
14 |
--------------------------------------------------------------------------------
/tests/4-js-testing.svelte:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/helpers/browser.js:
--------------------------------------------------------------------------------
1 | var path = require('path');
2 | var fs = require('fs-extra');
3 |
4 | const TESTS_DIR = path.resolve('tests');
5 | const HTML_FILE = path.join(TESTS_DIR,'browser','index.html');
6 |
7 | const HJS_FILE = 'https://unpkg.com/highlightjs';
8 | const SVELTE_LANG_FILE = path.resolve(path.join('dist','svelte.min.js'));
9 | const CSS_FILE = path.resolve(path.join('node_modules','highlight.js','styles','rainbow.css'));
10 |
11 | module.exports.createBrowserTest = function(snaplist){
12 | const body = snaplist.reduce((acc,snapshot)=>{
13 | return acc + `
14 | ${snapshot.name}
15 | ${snapshot.source.replace(//g,'>')}
16 |
17 | `;
18 | },'');
19 |
20 |
21 | fs.ensureFileSync(HTML_FILE);
22 | fs.writeFileSync(HTML_FILE,`
23 |
24 |
25 |
26 | Browser Test page
27 |
28 |
29 |
30 |
31 |
32 |
33 | ${body}
34 |
35 |
36 | `);
37 |
38 | console.log('Open browser test on: '+HTML_FILE);
39 | }
40 |
--------------------------------------------------------------------------------
/tests/helpers/snapshots.js:
--------------------------------------------------------------------------------
1 | var path = require('path');
2 | var fs = require('fs-extra');
3 |
4 | const TESTS_DIR = path.resolve('tests');
5 | const SNAPSHOTS_DIR = path.join(TESTS_DIR,'snapshots');
6 | const TEST_EXT = '.svelte';
7 | const SNAPSHOT_EXT = '.html';
8 |
9 | module.exports.getSnaplist = function(handler,update=false){
10 |
11 | if(update) fs.emptyDirSync(SNAPSHOTS_DIR);
12 |
13 | const files = fs.readdirSync(TESTS_DIR).filter(f => f.endsWith(TEST_EXT));
14 |
15 | let snaplist = [];
16 | for(let i=0; i < files.length; i++){
17 | const name = path.basename(files[i],TEST_EXT);
18 | const source = fs.readFileSync(path.join(TESTS_DIR,files[i]),'utf-8');
19 | const result = handler(source);
20 | const snapshot = module.exports.getSnapshot(name,result);
21 |
22 | snaplist.push({name,source,result,snapshot});
23 | }
24 |
25 | return snaplist;
26 | }
27 |
28 | module.exports.getSnapshot = function (name,initial){
29 | const snapshot_file = path.join(SNAPSHOTS_DIR,name+SNAPSHOT_EXT);
30 |
31 | if(fs.existsSync(snapshot_file))
32 | return fs.readFileSync(snapshot_file,'utf-8');
33 | else
34 | return module.exports.updateSnapshot(name,initial);
35 | }
36 |
37 | module.exports.updateSnapshot = function (name,data){
38 | const snapshot_file = path.join(SNAPSHOTS_DIR,name+SNAPSHOT_EXT);
39 | fs.ensureFileSync(snapshot_file);
40 | fs.writeFileSync(snapshot_file,data);
41 | return data;
42 | }
--------------------------------------------------------------------------------
/tests/helpers/testing.js:
--------------------------------------------------------------------------------
1 | var test = require('tape');
2 |
3 | module.exports.doTesting = function(snaplist){
4 | snaplist.forEach(snapshot => {
5 | test(snapshot.name, function (t) {
6 | t.equal(snapshot.result,snapshot.snapshot);
7 | t.end();
8 | });
9 | })
10 | }
11 |
--------------------------------------------------------------------------------
/tests/helpers/visual.js:
--------------------------------------------------------------------------------
1 | var path = require('path');
2 | var fs = require('fs-extra');
3 |
4 | const TESTS_DIR = path.resolve('tests');
5 | const HTML_FILE = path.join(TESTS_DIR,'visual','index.html');
6 | const CSS_FILE = path.resolve(path.join('node_modules','highlight.js','styles','rainbow.css'));
7 |
8 | module.exports.createVisualTest = function(snaplist){
9 | const body = snaplist.reduce((acc,snapshot)=>{
10 | return acc + `
11 | ${snapshot.name}
12 | ${snapshot.result}
13 |
14 | `;
15 | },'');
16 |
17 | const CSS = fs.readFileSync(CSS_FILE,'utf-8');
18 |
19 | fs.ensureFileSync(HTML_FILE);
20 | fs.writeFileSync(HTML_FILE,`
21 |
22 |
23 |
24 | Visual Test page
25 |
26 |
29 |
30 | ${body}
31 |
32 |
33 | `);
34 |
35 | console.log('Open visual test on: '+HTML_FILE);
36 | }
37 |
--------------------------------------------------------------------------------
/tests/snapshots/1-simple-component.html:
--------------------------------------------------------------------------------
1 | <script>
2 | export let name = 'world';
3 | </script>
4 |
5 | <h1>Hello, {name}!</h1>
6 |
7 | <style>
8 | h1{
9 | color:red;
10 | }
11 | </style>
--------------------------------------------------------------------------------
/tests/snapshots/2-pngwn-example.html:
--------------------------------------------------------------------------------
1 | {#each array.map(({a, b, c}) => ({ a: a+1, b, c})) as {a, b, c} }
2 | {name === undefined ? 'Anybody' : name}
3 | {/each}
--------------------------------------------------------------------------------
/tests/snapshots/3-most-common-component.html:
--------------------------------------------------------------------------------
1 | <script>
2 | export let list = ['item1','item2',`item${3}`,4];
3 | export let name = 'World';
4 | $: num = list.length;
5 | </script>
6 |
7 | <h1>Hello, {name}!</h1>
8 | {#each list as item}
9 | <p>{ typeof item === 'str`===ing?' ? item.toUpperCase() : item}</p>
10 | {:else}
11 | <p>No items...</p>
12 | {/each}
13 |
14 | <style>
15 | h1{color:red;}
16 | </style>
--------------------------------------------------------------------------------
/tests/snapshots/4-js-testing.html:
--------------------------------------------------------------------------------
1 | <script>
2 | function $initHighlight(block, cls) {
3 | try {
4 | if (cls.search(/\bno\-highlight\b/) != -1)
5 | return process(block, true, 0x0F) +
6 | ` class="${cls}"`;
7 | } catch (e) {
8 |
9 | }
10 | for (var i = 0 / 2; i < classes.length; i++) {
11 | if (checkCondition(classes[i]) === undefined)
12 | console.log('undefined');
13 | }
14 |
15 | return (
16 | <div>
17 | <web-component>{block}</web-component>
18 | </div>
19 | )
20 | }
21 |
22 | export $initHighlight;
23 | </script>
--------------------------------------------------------------------------------
/tests/test.js:
--------------------------------------------------------------------------------
1 | const command = process.argv[2];
2 |
3 | const {getSnaplist} = require('./helpers/snapshots');
4 | const {createBrowserTest} = require('./helpers/browser');
5 | const {createVisualTest} = require('./helpers/visual');
6 | const {doTesting} = require('./helpers/testing');
7 |
8 | const hljs = require('highlight.js');
9 | const hljs_svelte = require('../dist/index.js');
10 | hljs_svelte(hljs);
11 |
12 |
13 | function handleSource(source){
14 | return hljs.highlight('svelte', source).value;
15 | }
16 |
17 |
18 |
19 | const snaplist = getSnaplist(handleSource,(command === 'update'));
20 | createBrowserTest(snaplist);
21 | createVisualTest(snaplist);
22 | doTesting(snaplist);
23 |
24 |
--------------------------------------------------------------------------------