示例#1
0
        private string Preview(string workspacePath, string relativePath, string markdownContent,
                               bool isFirstTime     = false, string previewFilePath = null, string pageUpdateJsFilePath = null,
                               string builtHtmlPath = null)
        {
            if (string.IsNullOrEmpty(workspacePath))
            {
                throw new HandlerClientException("Base directory should not be null or empty");
            }
            if (string.IsNullOrEmpty(relativePath))
            {
                throw new HandlerClientException("Relative path should not be null or empty");
            }
            var markupResult = DfmMarkup(workspacePath, relativePath, markdownContent);

            if (!isFirstTime)
            {
                return(markupResult);
            }

            if (string.IsNullOrEmpty(builtHtmlPath))
            {
                throw new HandlerClientException("Built Html path should not be null or empty");
            }
            if (string.IsNullOrEmpty(pageUpdateJsFilePath))
            {
                throw new HandlerClientException("Page update js file path should not be null or empty");
            }

            PreviewJsonConfig config = PreviewCommand.ParsePreviewCommand(workspacePath);

            builtHtmlPath        = new Uri(builtHtmlPath).LocalPath;
            pageUpdateJsFilePath = new Uri(pageUpdateJsFilePath).LocalPath;
            previewFilePath      = new Uri(previewFilePath).LocalPath;

            string htmlString = File.ReadAllText(builtHtmlPath);

            CQ dom = htmlString;

            CQ addElements = $"<script type='text/javascript' src='{pageUpdateJsFilePath}'></script>" +
                             $"<meta name='pageRefreshFunctionName' content ='{config.PageRefreshFunctionName}'>" +
                             $"<meta name='port' content='{config.NavigationPort}'>" +
                             $"<meta name='filePath' content='{relativePath}'>" +
                             $"<meta name='markupTagType' content='{config.MarkupTagType}'>" +
                             $"<meta name='markupClassName' content='{config.MarkupClassName}'>";

            foreach (var addElement in addElements)
            {
                dom.Select(addElement.NodeName)
                .Last()
                .After(addElement);
            }

            // Replace 'https' to 'http' for that VS Code don't support reference which use https protocol now
            // Replace reference relative path to absolute path
            foreach (var item in config.References)
            {
                dom.Select(item.Key).Each((i, e) =>
                {
                    var path = e.GetAttribute(item.Value);
                    if (string.IsNullOrEmpty(path))
                    {
                        return;
                    }
                    // VSCode bug: https://github.com/Microsoft/vscode/issues/23020
                    // Remove when bug fixed
                    if (path.StartsWith("http"))
                    {
                        if (path.StartsWith("https"))
                        {
                            e.SetAttribute(item.Value, ReplaceFirstOccurrence(path, "https", "http"));
                        }
                        return;
                    }
                    if (Path.IsPathRooted(path))
                    {
                        e.SetAttribute(item.Value, path);
                    }
                    else
                    {
                        e.SetAttribute(item.Value, GetAbsolutePath(builtHtmlPath, path));
                    }
                });
            }

            // For Docs
            // Replace toc relative path to absolute path
            dom.Select("meta").Each((i, e) =>
            {
                // TODO: Implement breadcrumb
                var metaName = e.GetAttribute("name");
                if (metaName == config.TocMetadataName)
                {
                    e.SetAttribute("content", GetAbsolutePath(builtHtmlPath, e.GetAttribute("content")));
                }
            });

            File.WriteAllText(previewFilePath, dom.Render());
            return(markupResult);
        }
示例#2
0
        private string Preview(CommandMessage contextMessage)
        {
            if (string.IsNullOrEmpty(contextMessage.WorkspacePath))
            {
                throw new HandlerClientException("Base directory should not be null or empty");
            }
            if (string.IsNullOrEmpty(contextMessage.RelativePath))
            {
                throw new HandlerClientException("Relative path should not be null or empty");
            }
            string result = DfmMarkup(contextMessage.WorkspacePath, contextMessage.RelativePath, contextMessage.MarkdownContent);

            if (contextMessage.ShouldSeparateMarkupResult)
            {
                var htmlInfo = HtmlDocumentUtility.SeparateHtml(result);
                var separatedMarkupResult =
                    new { rawTitle = htmlInfo.RawTitle, contentWithoutRawTitle = htmlInfo.Content };
                result = JsonConvert.SerializeObject(separatedMarkupResult);
            }
            if (string.IsNullOrEmpty(contextMessage.TempPreviewFilePath))
            {
                return(result);
            }

            // TODO: move this part to client
            if (string.IsNullOrEmpty(contextMessage.OriginalHtmlPath))
            {
                throw new HandlerClientException("Built Html path should not be null or empty");
            }
            if (string.IsNullOrEmpty(contextMessage.PageRefreshJsFilePath))
            {
                throw new HandlerClientException("Page update js file path should not be null or empty");
            }

            PreviewJsonConfig config = PreviewCommand.ParsePreviewCommand(contextMessage.WorkspacePath);

            var originalHtmlPath      = new Uri(contextMessage.OriginalHtmlPath).LocalPath;
            var pageRefreshJsFilePath = new Uri(contextMessage.PageRefreshJsFilePath).LocalPath;
            var tempPreviewFilePath   = new Uri(contextMessage.TempPreviewFilePath).LocalPath;

            string htmlString = File.ReadAllText(originalHtmlPath);

            CQ dom = htmlString;

            CQ addElements = $"<script type='text/javascript' src='{pageRefreshJsFilePath}'></script>" +
                             $"<meta name='port' content='{contextMessage.NavigationPort}'>" +
                             $"<meta name='filePath' content='{contextMessage.RelativePath}'>";

            foreach (var addElement in addElements)
            {
                dom.Select(addElement.NodeName)
                .Last()
                .After(addElement);
            }

            // Replace 'https' to 'http' for that VS Code don't support reference which use https protocol now
            // Replace reference relative path to absolute path
            foreach (var item in config.References)
            {
                dom.Select(item.Key).Each((i, e) =>
                {
                    var path = e.GetAttribute(item.Value);
                    if (string.IsNullOrEmpty(path))
                    {
                        return;
                    }
                    // VSCode bug: https://github.com/Microsoft/vscode/issues/23020
                    // Remove when bug fixed
                    if (path.StartsWith("http"))
                    {
                        if (path.StartsWith("https"))
                        {
                            e.SetAttribute(item.Value, ReplaceFirstOccurrence(path, "https", "http"));
                        }
                        return;
                    }
                    if (Path.IsPathRooted(path))
                    {
                        e.SetAttribute(item.Value, path);
                    }
                    else
                    {
                        e.SetAttribute(item.Value, GetAbsolutePath(originalHtmlPath, path));
                    }
                });
            }

            // For Docs
            // Replace toc relative path to absolute path
            dom.Select("meta").Each((i, e) =>
            {
                // TODO: Implement breadcrumb
                var metaName = e.GetAttribute("name");
                if (metaName == config.TocMetadataName)
                {
                    e.SetAttribute("content", GetAbsolutePath(originalHtmlPath, e.GetAttribute("content")));
                }
            });

            File.WriteAllText(tempPreviewFilePath, dom.Render());
            return(result);
        }