Ejemplo n.º 1
0
        public void Register(IAppHost appHost)
        {
            if (instance == null)
            {
                instance = this;
            }

            this.AppHost = appHost;
            appHost.ViewEngines.Add(this);

            if (!WatchForModifiedPages)
            {
                WatchForModifiedPages = appHost.Config.DebugMode;
            }

            foreach (var ns in EndpointHostConfig.RazorNamespaces)
            {
                Evaluator.AddAssembly(ns);
            }

            this.MarkdownBaseType      = appHost.Config.MarkdownBaseType ?? this.MarkdownBaseType;
            this.MarkdownGlobalHelpers = appHost.Config.MarkdownGlobalHelpers ?? this.MarkdownGlobalHelpers;

            this.ReplaceTokens = appHost.Config.HtmlReplaceTokens ?? new Dictionary <string, string>();
            var webHostUrl = appHost.Config.WebHostUrl;

            if (!webHostUrl.IsNullOrEmpty())
            {
                this.ReplaceTokens["~/"] = webHostUrl.WithTrailingSlash();
            }

            if (VirtualPathProvider == null)
            {
                VirtualPathProvider = AppHost.VirtualPathProvider;
            }

            RegisterMarkdownPages(appHost.Config.WebHostPhysicalPath);

            appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) => {
                MarkdownPage markdownPage = null;

                if (catchAllPathsNotFound.Contains(pathInfo))
                {
                    return(null);
                }

                markdownPage = FindByPathInfo(pathInfo);

                if (WatchForModifiedPages)
                {
                    ReloadModifiedPageAndTemplates(markdownPage);
                }

                if (markdownPage == null)
                {
                    if (pathInfo.EndsWith(".md"))
                    {
                        pathInfo = pathInfo.EndsWith(DefaultPage + ".md", StringComparison.InvariantCultureIgnoreCase)
                            ? pathInfo.Substring(0, pathInfo.Length - (DefaultPage + ".md").Length)
                            : pathInfo.WithoutExtension();

                        return(new RedirectHttpHandler {
                            AbsoluteUrl = webHostUrl.IsNullOrEmpty()
                                ? null
                                : webHostUrl.CombineWith(pathInfo),
                            RelativeUrl = webHostUrl.IsNullOrEmpty()
                                ? pathInfo
                                : null
                        });
                    }

                    if (catchAllPathsNotFound.Count > 1000) //prevent DDOS
                    {
                        catchAllPathsNotFound = new HashSet <string>();
                    }

                    var tmp = new HashSet <string>(catchAllPathsNotFound)
                    {
                        pathInfo
                    };
                    catchAllPathsNotFound = tmp;
                    return(null);
                }

                return(new MarkdownHandler(pathInfo)
                {
                    MarkdownFormat = this,
                    MarkdownPage = markdownPage,
                    RequestName = "MarkdownPage"
                });
            });

            appHost.ContentTypeFilters.Register(ContentType.MarkdownText, SerializeToStream, null);
            appHost.ContentTypeFilters.Register(ContentType.PlainText, SerializeToStream, null);
            appHost.Config.IgnoreFormatsInMetadata.Add(ContentType.MarkdownText.ToContentFormat());
            appHost.Config.IgnoreFormatsInMetadata.Add(ContentType.PlainText.ToContentFormat());
        }
Ejemplo n.º 2
0
        public void Register(IAppHost appHost)
        {
            if (instance == null)
            {
                instance = this;
            }

            this.AppHost = appHost;
            appHost.ViewEngines.Add(this);

            if (!CheckLastModifiedForChanges)
            {
                CheckLastModifiedForChanges = appHost.Config.DebugMode;
            }

            foreach (var ns in appHost.Config.RazorNamespaces)
            {
                Evaluator.AddAssembly(ns);
            }

            this.ReplaceTokens = appHost.Config.HtmlReplaceTokens ?? new Dictionary <string, string>();
            var webHostUrl = appHost.Config.WebHostUrl;

            if (!webHostUrl.IsNullOrEmpty())
            {
                this.ReplaceTokens["~/"] = webHostUrl.WithTrailingSlash();
            }

            if (VirtualPathProvider == null)
            {
                VirtualPathProvider = AppHost.VirtualFileSources;
            }

            RegisterMarkdownPages(appHost.WebHostPhysicalPath);

            appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) => {
                MarkdownPage markdownPage = null;

                if (catchAllPathsNotFound.Contains(pathInfo))
                {
                    return(null);
                }

                markdownPage = FindByPathInfo(pathInfo);

                if (CheckLastModifiedForChanges)
                {
                    ReloadModifiedPageAndTemplates(markdownPage);
                }

                if (markdownPage == null)
                {
                    if (pathInfo.EndsWith(".md"))
                    {
                        pathInfo = pathInfo.EndsWithIgnoreCase(DefaultPage + ".md")
                                                        ? pathInfo.Substring(0, pathInfo.Length - (DefaultPage + ".md").Length)
                                                        : pathInfo.WithoutExtension();

                        return(new RedirectHttpHandler {
                            AbsoluteUrl = webHostUrl.IsNullOrEmpty()
                                                                ? null
                                                                : webHostUrl.AppendPath(pathInfo),
                            RelativeUrl = webHostUrl.IsNullOrEmpty()
                                                                ? pathInfo
                                                                : null
                        });
                    }

                    if (catchAllPathsNotFound.Count > 1000)                     //prevent DDOS
                    {
                        catchAllPathsNotFound = new HashSet <string>();
                    }

                    var tmp = new HashSet <string>(catchAllPathsNotFound)
                    {
                        pathInfo
                    };
                    catchAllPathsNotFound = tmp;
                    return(null);
                }

                return(new MarkdownHandler(pathInfo)
                {
                    MarkdownFormat = this,
                    MarkdownPage = markdownPage,
                    RequestName = "MarkdownPage"
                });
            });

            appHost.ContentTypes.RegisterAsync(MimeTypes.MarkdownText, SerializeToStreamAsync, null);
            appHost.ContentTypes.RegisterAsync(MimeTypes.PlainText, SerializeToStreamAsync, null);
            appHost.Config.IgnoreFormatsInMetadata.Add(MimeTypes.MarkdownText.ToContentFormat());
            appHost.Config.IgnoreFormatsInMetadata.Add(MimeTypes.PlainText.ToContentFormat());

            appHost.GetPlugin <MetadataFeature>()
            ?.AddLink(MetadataFeature.AvailableFeatures, "http://docs.servicestack.net/markdown-razor", "Markdown Razor");
        }