Ejemplo n.º 1
0
            public async Task Should_parse_markdown_file_and_set_package_with_fully_resolved_path()
            {
                var workingDir = TestAssets.SampleConsole;
                var packagePathRelativeToBaseDir = "src/sample/sample.csproj";

                var dirAccessor = new InMemoryDirectoryAccessor(workingDir)
                {
                    ("src/sample/Program.cs", ""),
                    (packagePathRelativeToBaseDir, ""),
                    ("docs/Readme.md",
                     $@"```cs --project ../{packagePathRelativeToBaseDir} --source-file ../src/sample/Program.cs
```")
                };

                var project = new MarkdownProject(dirAccessor, Default.PackageFinder);

                project.TryGetMarkdownFile(new RelativeFilePath("docs/Readme.md"), out var markdownFile).Should().BeTrue();
                var htmlDocument = new HtmlDocument();

                htmlDocument.LoadHtml((await markdownFile.ToHtmlContentAsync()).ToString());
                var output = htmlDocument.DocumentNode
                             .SelectSingleNode("//pre/code").Attributes["data-trydotnet-package"];

                var fullProjectPath = dirAccessor.GetFullyQualifiedPath(new RelativeFilePath(packagePathRelativeToBaseDir));

                output.Value.Should().Be(fullProjectPath.FullName);
            }
Ejemplo n.º 2
0
        public async Task <IActionResult> ShowMarkdownFile(string path)
        {
            if (_startupOptions.Mode != StartupMode.Try)
            {
                return(NotFound());
            }

            if (string.IsNullOrEmpty(path))
            {
                const string documentSvg = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path d=\"M6,2A2,2 0 0,0 4,4V20A2,2 0 0,0 6,22H18A2,2 0 0,0 20,20V8L14,2H6M6,4H13V9H18V20H6V4M8,12V14H16V12H8M8,16V18H13V16H8Z\" /></svg>";
                var          links       = string.Join(
                    "\n",
                    _markdownProject.GetAllMarkdownFiles()
                    .Select(f =>
                            $@"<li><a href=""{f.Path.Value.HtmlAttributeEncode()}"">{documentSvg}<span>{f.Path.Value}</span></a></li>"));

                return(Content(Index(links).ToString(), "text/html"));
            }

            var relativeFilePath = new RelativeFilePath(path);

            if (!_markdownProject.TryGetMarkdownFile(relativeFilePath, out var markdownFile))
            {
                return(NotFound());
            }

            var hostUrl = Request.GetUri();

            var blocks = (await markdownFile.GetEditableAnnotatedCodeBlocks()).ToArray();

            var maxEditorPerSession = blocks.Length > 0
                                          ? blocks
                                      .GroupBy(b => b.Annotations.Session)
                                      .Max(editors => editors.Count())
                                          : 0;

            var pipeline = _markdownProject.GetMarkdownPipelineFor(markdownFile.Path);

            var extension = pipeline.Extensions.FindExact <CodeBlockAnnotationExtension>();

            if (extension != null)
            {
                extension.InlineControls        = maxEditorPerSession <= 1;
                extension.EnablePreviewFeatures = _startupOptions.EnablePreviewFeatures;
            }



            var content = maxEditorPerSession <= 1
                              ? await OneColumnLayoutScaffold(
                $"{hostUrl.Scheme}://{hostUrl.Authority}",
                markdownFile)
                              : await TwoColumnLayoutScaffold(
                $"{hostUrl.Scheme}://{hostUrl.Authority}",
                markdownFile);

            return(Content(content.ToString(), "text/html"));
        }
Ejemplo n.º 3
0
            public async Task Returns_false_for_nonexistent_file()
            {
                var workingDir  = TestAssets.SampleConsole;
                var dirAccessor = new InMemoryDirectoryAccessor(workingDir);
                var project     = new MarkdownProject(dirAccessor, await Default.PackageRegistry.ValueAsync());
                var path        = new RelativeFilePath("DOESNOTEXIST");

                project.TryGetMarkdownFile(path, out _).Should().BeFalse();
            }
Ejemplo n.º 4
0
            public void Returns_false_for_nonexistent_file()
            {
                var workingDir  = TestAssets.SampleConsole;
                var dirAccessor = new InMemoryDirectoryAccessor(workingDir);
                var project     = new MarkdownProject(dirAccessor, PackageRegistry.CreateForHostedMode());
                var path        = new RelativeFilePath("DOESNOTEXIST");

                project.TryGetMarkdownFile(path, out _).Should().BeFalse();
            }
Ejemplo n.º 5
0
        public async Task <IActionResult> ShowMarkdownFile(string path)
        {
            if (_startupOptions.Mode != StartupMode.Try)
            {
                return(NotFound());
            }

            var relativeFilePath = new RelativeFilePath(path);

            if (!_markdownProject.TryGetMarkdownFile(relativeFilePath, out var markdownFile))
            {
                return(NotFound());
            }

            var hostUrl = Request.GetUri();

            var blocks = (await markdownFile.GetEditableAnnotatedCodeBlocks()).ToArray();

            var maxEditorPerSession = blocks.Length > 0
                                          ? blocks
                                      .GroupBy(b => b.Annotations.Session)
                                      .Max(editors => editors.Count())
                                          : 0;

            var pipeline = _markdownProject.GetMarkdownPipelineFor(markdownFile.Path);

            var extension = pipeline.Extensions.FindExact <CodeBlockAnnotationExtension>();

            if (extension != null)
            {
                extension.InlineControls        = maxEditorPerSession <= 1;
                extension.EnablePreviewFeatures = _startupOptions.EnablePreviewFeatures;
            }



            var content = maxEditorPerSession <= 1
                              ? await OneColumnLayoutScaffold(
                $"{hostUrl.Scheme}://{hostUrl.Authority}",
                markdownFile)
                              : await TwoColumnLayoutScaffold(
                $"{hostUrl.Scheme}://{hostUrl.Authority}",
                markdownFile);

            return(Content(content.ToString(), "text/html"));
        }