internal static void HighlightElement(IJavaScriptEnginePool enginePool, AngleSharp.Dom.IElement element)
        {
            using (IJavaScriptEngine engine = enginePool.GetEngine())
            {
                // Make sure to use TextContent, otherwise you'll get escaped html which highlight.js won't parse
                engine.SetVariableValue("input", element.TextContent);

                // Check if they specified a language in their code block
                string language = element.ClassList.FirstOrDefault(i => i.StartsWith("language"));
                if (language != null)
                {
                    engine.SetVariableValue("language", language.Replace("language-", string.Empty));
                    engine.Execute("result = hljs.highlight(language, input)");
                }
                else
                {
                    language = "(auto)"; // set this to auto in case there is an exception below
                    engine.Execute("result = hljs.highlightAuto(input)");
                    string detectedLanguage = engine.Evaluate <string>("result.language");
                    if (!string.IsNullOrWhiteSpace(detectedLanguage))
                    {
                        element.ClassList.Add("language-" + detectedLanguage);
                    }
                }

                element.ClassList.Add("hljs");
                element.InnerHtml = engine.Evaluate <string>("result.value");
            }
        }
        public override async Task <IApiResponse> ExecuteAsync(IApiRequest request)
        {
            if (String.IsNullOrEmpty(_command.Script))
            {
                throw new RequestModelException($"'script' must be specified for script command");
            }
            String code = await _reader.ReadTextFileAsync(_command.Path, _command.Script.AddExtension("js"));

            _script.SetCurrentDirectory(_command.Path);
            var args = new ExpandoObject();

            if (request.Body != null)
            {
                args.Set("body", request.Body);
            }
            if (request.Query != null)
            {
                args.Set("query", request.Query);
            }
            if (!String.IsNullOrEmpty(_command.Id))
            {
                args.Set("id", _command.Id);
            }

            var prms = _command.Parameters.Clone(null);

            if (request.UserId != 0)
            {
                prms.Set("UserId", request.UserId);
            }
            if (request.TenantId != null)
            {
                prms.Set("TenantId", request.TenantId);
            }
            if (!String.IsNullOrEmpty(request.Segment))
            {
                prms.Set("Segment", request.Segment);
            }

            var obj = _script.Execute(code, prms, args);

            return(new ApiResponse()
            {
                ContentType = MimeTypes.Application.Json,
                Body = JsonConvert.SerializeObject(Wrap(obj), JsonHelpers.CompactSerializerSettings)
            });
        }
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            if (String.IsNullOrEmpty(cmd.file))
            {
                throw new RequestModelException($"'file' must be specified for command '{cmd.command}'");
            }
            String code = await _reader.ReadTextFileAsync(cmd.Path, cmd.file.AddExtension("js"));

            if (code == null)
            {
                throw new RequestModelException($"File not found '{cmd.file}'");
            }
            _engine.SetCurrentDirectory(cmd.Path);
            var retval = _engine.Execute(code, dataToExec, cmd.args);

            return(new ServerCommandResult()
            {
                Data = JsonConvert.SerializeObject(retval, JsonHelpers.StandardSerializerSettings)
            });
        }