Skip to content

Commit d31d460

Browse files
committed
Added basic impl
1 parent 90bbf93 commit d31d460

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

dist/highlightjs-lang.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var gulp = require('gulp');
2+
var uglify = require('gulp-uglify');
3+
var rename = require("gulp-rename");
4+
5+
gulp.task('build', function() {
6+
return gulp.src('src/*.js')
7+
.pipe(uglify())
8+
.pipe(rename({
9+
extname: '.min.js'
10+
}))
11+
.pipe(gulp.dest('dist'));
12+
});

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "highlightjs-lang.js",
3+
"version": "1.0.0",
4+
"description": "Highlight.js plugin for show of used syntax language.",
5+
"main": "gulpfile.js",
6+
"dependencies": {},
7+
"devDependencies": {
8+
"gulp": "^3.8.11",
9+
"gulp-rename": "^1.2.2",
10+
"gulp-uglify": "^1.2.0"
11+
},
12+
"scripts": {
13+
"test": "echo \"Error: no test specified\" && exit 1"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "https://github.com/wcoder/highlightjs-lang.js.git"
18+
},
19+
"author": "Yauheni Pakala <[email protected]>",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/wcoder/highlightjs-lang.js.git/issues"
23+
},
24+
"homepage": "https://github.com/wcoder/highlightjs-lang.js.git"
25+
}

src/highlightjs-lang.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
(function (w) {
2+
'use strict';
3+
4+
if (typeof w.hljs === 'undefined') {
5+
console.error('highlight.js not detected!');
6+
} else {
7+
w.hljs.initLineNumbersOnLoad = initLineNumbersOnLoad;
8+
w.hljs.lineNumbersBlock = lineNumbersBlock;
9+
}
10+
11+
function initLineNumbersOnLoad () {
12+
w.addEventListener('load', function () {
13+
try {
14+
var blocks = document.querySelectorAll('code.hljs');
15+
16+
for (var i in blocks) {
17+
if (blocks.hasOwnProperty(i)) {
18+
lineNumbersBlock(blocks[i]);
19+
}
20+
}
21+
} catch (e) {
22+
console.error('highlight-lang error: ', e);
23+
}
24+
});
25+
}
26+
27+
function lineNumbersBlock (element) {
28+
if (typeof element !== 'object') return;
29+
30+
var classes = element.className.split(' ');
31+
var lang = getLangNameFromClasses(classes);
32+
33+
if (lang !== '') {
34+
var langPanel = document.createElement('div');
35+
langPanel.className = 'hljs-lang';
36+
langPanel.textContent = convertLangName(lang);
37+
element.parentNode.insertBefore(langPanel, element);
38+
}
39+
}
40+
41+
function getLangNameFromClasses(classes) {
42+
// TODO: define lang for auto-syntax
43+
if (!!classes && classes.length > 1 && classes[1] === 'hljs') {
44+
return classes[0];
45+
}
46+
return '';
47+
}
48+
49+
function convertLangName(lang)
50+
{
51+
// TODO: add more
52+
var map = [
53+
['C#', ['cs', 'csharp']],
54+
['F#', ['fsharp']],
55+
['Objective-C', ['objectivec']]
56+
];
57+
58+
map.forEach(function (e) {
59+
if (e[1].indexOf(lang) !== -1) {
60+
lang = e[0];
61+
return;
62+
}
63+
});
64+
return lang;
65+
}
66+
67+
}(window));

0 commit comments

Comments
 (0)