Ejemplo n.º 1
0
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var pathString = hcx.HttpContext.Request.Path;

            if (pathString.Value.Contains("/redoc/"))
            {
                swaggerDoc.Info.Title   = docOptions.Title;
                swaggerDoc.Info.Version = docOptions.Version;
                swaggerDoc.Tags         = swaggerDoc.Tags ?? new List <OpenApiTag>();
                var existingTags = new List <string>(swaggerDoc.Tags.Select(x => x.Name));
                swaggerDoc.Info.Description = null;
                var markdown  = markdownHandler.GetMarkdownDocuments();
                var tagGroups = new List <TagGroup>();
                foreach (var folder in markdown.OfType <WikiMarkdownFolder>())
                {
                    tagGroups.Add(new TagGroup
                    {
                        Name = folder.Name,
                        Tags = folder.Select(x => x.Name).ToOpenApiArray()
                    });

                    foreach (var file in folder)
                    {
                        var tag = new OpenApiTag
                        {
                            Name        = file.Name,
                            Description = file.Content
                        };
                        swaggerDoc.Tags.Add(tag);
                    }
                }

                if (markdown.OfType <WikiMarkdownFile>().Any())
                {
                    var miscTagGroup = new TagGroup {
                        Name = options.WikiRootFilesFolderName, Tags = new OpenApiArray()
                    };
                    tagGroups.Add(miscTagGroup);
                    foreach (var file in markdown.OfType <WikiMarkdownFile>())
                    {
                        var tag = new OpenApiTag
                        {
                            Name        = file.Name,
                            Description = file.Content
                        };
                        miscTagGroup.Tags.Add(new OpenApiString(file.Name));
                        swaggerDoc.Tags.Add(tag);
                    }
                }

                var additions = options.AdditionalControllersToInclude.Except(existingTags, System.StringComparer.OrdinalIgnoreCase).ToList();
                existingTags.AddRange(additions);
                tagGroups.Add(
                    new TagGroup
                {
                    Name = options.ApiReferenceGroupName,
                    Tags = existingTags.ToOpenApiArray()
                });

                swaggerDoc.Extensions.Add("x-tagGroups", tagGroups.ToOpenApiArray());

                if (!swaggerDoc.Info.Extensions.ContainsKey("x-logo") && !string.IsNullOrWhiteSpace(options.LogoUrl))
                {
                    swaggerDoc.Info.Extensions.Add("x-logo", new Logo
                    {
                        AltText         = options.LogoAltText,
                        BackgroundColor = options.LogoBackgroundColor,
                        Url             = options.LogoUrl
                    });
                }
            }
        }
Ejemplo n.º 2
0
        private string BuildWikiHtmlSection()
        {
            var swaggerNav     = new StringBuilder();
            var swaggerContent = new StringBuilder();
            var markdown       = markdownHandler.GetMarkdownDocuments();
            var template       = options.MainTemplate;
            var navCount       = 0;

            var mpb = new MarkdownPipelineBuilder();

            mpb.UseEmojiAndSmiley();
            var mp = mpb.Build();

            foreach (var node in markdown)
            {
                navCount++;
                swaggerNav.AppendLine(options.NavBarHeadingTemplate.Replace("{{name}}", node.Name).Replace("{{nav-bar-item-wrapper-class}}", " nav-level-0"));
                swaggerContent.AppendLine($"<span id='wiki-section-{node.Name}'></span>");

                if (node is WikiMarkdownFolder folder)
                {
                    foreach (var file in folder.Where(x => x.Name == folder.Name))
                    {
                        // No nav item for the same-name file, and always put the file at the top of the folder section.
                        var html = Markdown.ToHtml(file.Content, mp);
                        html = InsertHeadingTagIds(swaggerNav, file, html, 2);
                        swaggerContent.AppendLine(options.ContentTemplate.Replace("{{name}}", file.Name).Replace("{{ref-type}}", idSlug).Replace("{{content}}", html));
                    }
                    foreach (var file in folder.Where(x => x.Name != folder.Name))
                    {
                        var html = Markdown.ToHtml(file.Content, mp);
                        navCount++;
                        swaggerNav.AppendLine(options.NavBarItemTemplate
                                              .Replace("{{name}}", file.Name)
                                              .Replace("{{ref-type}}", idSlug)
                                              .Replace("{{nav-bar-item-wrapper-class}}", " nav-level-1"));

                        html = InsertHeadingTagIds(swaggerNav, file, html, 2);
                        swaggerContent.AppendLine(options.ContentTemplate.Replace("{{name}}", file.Name).Replace("{{ref-type}}", idSlug).Replace("{{content}}", html));
                    }
                }

                if (node is WikiMarkdownFile rootFile)
                {
                    var html = Markdown.ToHtml(rootFile.Content, mp);
                    html = InsertHeadingTagIds(swaggerNav, rootFile, html, 1);
                    swaggerContent.AppendLine(options.ContentTemplate.Replace("{{name}}", rootFile.Name).Replace("{{ref-type}}", idSlug).Replace("{{content}}", html));
                }
            }

            if (navCount > 1)
            {
                template = template.Replace("{{wiki_nav_class}}", string.Empty);
            }
            else
            {
                template = template.Replace("{{wiki_nav_class}}", " hide-nav");
            }

            return(template.Replace("{{wiki_nav}}", swaggerNav.ToString()).Replace("{{wiki_content}}", swaggerContent.ToString()));
        }
Ejemplo n.º 3
0
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var pathString = hcx.HttpContext.Request.Path;

            if (pathString.Value.Contains("/redoc/"))
            {
                swaggerDoc.Info.Title   = docOptions.Title;
                swaggerDoc.Info.Version = docOptions.Version;
                swaggerDoc.Tags         = swaggerDoc.Tags ?? new List <OpenApiTag>();
                var existingTags = new List <string>(swaggerDoc.Tags.Select(x => x.Name));
                swaggerDoc.Info.Description = null;
                var markdown = markdownHandler.GetMarkdownDocuments();
                var tagGroup = new TagGroup
                {
                    Name = options.WikiGroupName,
                    Tags = new OpenApiArray()
                };
                foreach (var node in markdown)
                {
                    var content = new StringBuilder();
                    switch (node)
                    {
                    case WikiMarkdownFolder folder:
                        tagGroup.Tags.Add(new OpenApiString(folder.Name));
                        foreach (var file in folder.Where(x => x.Name == folder.Name))
                        {
                            content.AppendLine(file.Content);
                            content.AppendLine();
                            content.AppendLine();
                        }

                        foreach (var file in folder.Where(x => x.Name != folder.Name))
                        {
                            content.AppendLine($"# {file.Name}");
                            content.AppendLine(file.Content);
                            content.AppendLine();
                            content.AppendLine();
                        }
                        swaggerDoc.Tags.Add(new OpenApiTag
                        {
                            Name        = folder.Name,
                            Description = content.ToString()
                        });
                        break;

                    case WikiMarkdownFile rootFile:
                        tagGroup.Tags.Add(new OpenApiString(rootFile.Name));
                        content.AppendLine(rootFile.Content);
                        content.AppendLine();
                        content.AppendLine();
                        swaggerDoc.Tags.Add(new OpenApiTag
                        {
                            Name        = rootFile.Name,
                            Description = content.ToString()
                        });
                        break;
                    }
                }

                var additions = options.AdditionalControllersToInclude.Except(existingTags, System.StringComparer.OrdinalIgnoreCase).ToList();
                existingTags.AddRange(additions);

                swaggerDoc.Extensions.Add("x-tagGroups", new OpenApiArray
                {
                    tagGroup,
                    new TagGroup
                    {
                        Name = options.ApiReferenceGroupName,
                        Tags = existingTags.ToOpenApiArray()
                    }
                });

                if (!swaggerDoc.Info.Extensions.ContainsKey("x-logo") && !string.IsNullOrWhiteSpace(options.LogoUrl))
                {
                    swaggerDoc.Info.Extensions.Add("x-logo", new Logo
                    {
                        AltText         = options.LogoAltText,
                        BackgroundColor = options.LogoBackgroundColor,
                        Url             = options.LogoUrl
                    });
                }
            }
        }