Esempio n. 1
0
        public static PreviewJsonConfig ParsePreviewCommand(string baseDir)
        {
            string            configFilePath = Path.Combine(baseDir, PreviewConstants.ConfigFile);
            PreviewJsonConfig config         = null;

            try
            {
                if (File.Exists(configFilePath))
                {
                    config = JsonUtility.Deserialize <PreviewJsonConfig>(configFilePath);
                }
            }
            catch (JsonException e)
            {
                // TODO: reply to extension with an error message
                throw e;
            }
            return(MergeDefaultConfig(config));
        }
Esempio n. 2
0
        private static string DocfxProcessCore(string baseDir, string relativePath, string markdownContent,
                                               bool isFirstTime = false, string previewFilePath = null, string pageUpdateJsFilePath = null)
        {
            if (string.IsNullOrEmpty(baseDir))
            {
                throw new DocfxPreviewException("Base directory should not be null or empty");
            }
            if (string.IsNullOrEmpty(relativePath))
            {
                throw new DocfxPreviewException("Relative path should not be null or empty");
            }
            var markupResult = DfmMarkup(baseDir, relativePath, markdownContent);

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

            if (string.IsNullOrEmpty(previewFilePath))
            {
                throw new DocfxPreviewException("Preview file path should not be null or empty");
            }
            if (string.IsNullOrEmpty(pageUpdateJsFilePath))
            {
                throw new DocfxPreviewException("Page Update js file path should not be null or empty");
            }

            PreviewJsonConfig config = PreviewCommand.ParsePreviewCommand(baseDir);

            // Path of Html which generated from target markdown file
            string targetHtmlPath = Path.Combine(Path.GetDirectoryName(previewFilePath),
                                                 Path.GetFileNameWithoutExtension(relativePath) + ".html");

            if (string.IsNullOrEmpty(targetHtmlPath))
            {
                // TODO: If the return value is not a complete Html, it should be contacted with an Html header and tail
                DocfxRebuild();
                return(markupResult);
            }

            string htmlString = File.ReadAllText(targetHtmlPath);

            CQ dom = htmlString;

            CQ addElements = $"<script type='text/javascript' src='{pageUpdateJsFilePath}'></script>" +
                             $"<meta name='pageRefreshFunctionName' content ='{config.PageRefreshFunctionName}'>" +
                             $"<meta name='port' content='{config.Port}'>" +
                             $"<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;
                    }
                    if (path.StartsWith("https"))
                    {
                        e.SetAttribute(item.Value, ReplaceFirstOccurrence(path, "https", "http"));
                        return;
                    }

                    e.SetAttribute(item.Value, GetAbsolutePath(targetHtmlPath, 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(targetHtmlPath, e.GetAttribute("content")));
                }
            });

            File.WriteAllText(previewFilePath, dom.Render());
            return(markupResult);
        }
Esempio n. 3
0
        private static PreviewJsonConfig MergeDefaultConfig(PreviewJsonConfig config)
        {
            if (config == null)
            {
                config = new PreviewJsonConfig();
                config.BuildSourceFolder       = PreviewConstants.BuildSourceFolder;
                config.BuildOutputSubFolder    = PreviewConstants.BuildOutputSubfolder;
                config.MarkupTagType           = PreviewConstants.MarkupTagType;
                config.MarkupClassName         = PreviewConstants.MarkupClassName;
                config.OutputFolder            = PreviewConstants.OutputFolder;
                config.PageRefreshFunctionName = PreviewConstants.PageRefreshFunctionName;
                config.Port            = PreviewConstants.Port;
                config.References      = new Dictionary <string, string>(PreviewConstants.References);
                config.TocMetadataName = PreviewConstants.tocMetadataName;
                return(config);
            }

            if (string.IsNullOrEmpty(config.BuildSourceFolder))
            {
                config.BuildSourceFolder = PreviewConstants.BuildSourceFolder;
            }

            if (string.IsNullOrEmpty(config.BuildOutputSubFolder))
            {
                config.BuildOutputSubFolder = PreviewConstants.BuildOutputSubfolder;
            }

            if (string.IsNullOrEmpty(config.MarkupTagType))
            {
                config.MarkupTagType = PreviewConstants.MarkupTagType;
            }

            if (string.IsNullOrEmpty(config.MarkupClassName))
            {
                config.MarkupClassName = PreviewConstants.MarkupClassName;
            }

            if (string.IsNullOrEmpty(config.OutputFolder))
            {
                config.OutputFolder = PreviewConstants.OutputFolder;
            }

            if (string.IsNullOrEmpty(config.PageRefreshFunctionName))
            {
                config.PageRefreshFunctionName = PreviewConstants.PageRefreshFunctionName;
            }

            if (string.IsNullOrEmpty(config.Port))
            {
                config.Port = PreviewConstants.Port;
            }

            if (config.References == null)
            {
                config.References = new Dictionary <string, string>(PreviewConstants.References);
            }
            else
            {
                foreach (var reference in PreviewConstants.References)
                {
                    if (!config.References.ContainsKey(reference.Key))
                    {
                        config.References[reference.Key] = reference.Value;
                    }
                }
            }

            if (string.IsNullOrEmpty(config.TocMetadataName))
            {
                config.TocMetadataName = PreviewConstants.tocMetadataName;
            }

            return(config);
        }