-
Notifications
You must be signed in to change notification settings - Fork 1
/
HighlightJS.cs
53 lines (42 loc) · 1.94 KB
/
HighlightJS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.ClearScript.V8;
using AngleSharp.Dom;
using Microsoft.ClearScript;
namespace PowerSyntax
{
public class HighlightJS : IDisposable
{
public HighlightJS(IResourceProvider resource)
{
this.resource = resource;
engine = new V8ScriptEngine();
engine.AddHostObject("require", new Func<string, object>(x => {
string code = resource.ReadAllText(Path.Combine($@"\lib\{x.Substring(2)}.js"));
return engine.Evaluate(new DocumentInfo(x), @"(function(){ var exports = {}; var module = { exports: exports };" + code + @"; return exports === module.exports ? exports : module.exports; })()");
}));
hljs = engine.Script.require(@".\index");
var listLanguages = hljs.GetProperty("listLanguages") as ScriptObject;
var languagesArray = listLanguages.Invoke(false) as ScriptObject;
var length = languagesArray.GetProperty("length") as int? ?? 0;
languages = Enumerable.Range(0, length).Select(x => languagesArray.GetProperty(x).ToString()).ToArray();
highlightJsFunction = engine.Evaluate(@"((hljs, code, languageSubset) => hljs.highlightAuto(code, [languageSubset]).value)") as ScriptObject;
}
public void Dispose()
{
engine.Dispose();
}
public IList<string> ListLanguages() => languages;
public string HighlightAuto(string code, string language)
{
return highlightJsFunction.Invoke(false, hljs, code, language).ToString();
}
private V8ScriptEngine engine = null;
private ScriptObject highlightJsFunction = null;
private ScriptObject hljs = null;
private string[] languages = null;
private IResourceProvider resource = null;
}
}