Skip to content

Commit af05dc0

Browse files
committed
feat: added possibility to add a edit page
1 parent d525803 commit af05dc0

File tree

7 files changed

+43
-10
lines changed

7 files changed

+43
-10
lines changed

Source/Markdown.Renderer.Test/RendererTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public async Task BasicTest()
1111
var renderer = new HtmlRenderer();
1212
var sb = new StringBuilder();
1313
using var sw = new StringWriter(sb);
14-
await renderer.WriteHtml(sw, "test", "", 0, markdown);
14+
await renderer.WriteHtml(sw, "test", "", 0, markdown, "");
1515
var renderedHtml = sb.ToString();
1616

1717
Assert.NotEmpty(renderedHtml);

Source/Markdown.Renderer/HtmlRenderer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ public void PrepareBasePath(string basePath)
3939

4040
WriteCssFileTo(cssFilePath);
4141
}
42-
public async Task WriteHtml(TextWriter writer, string title, string description, int nestingDeep, string markdown)
42+
public async Task WriteHtml(
43+
TextWriter writer,
44+
string title,
45+
string description,
46+
int nestingDeep,
47+
string markdown,
48+
string editPageLink)
4349
{
4450
var prefix = string.Concat(Enumerable.Repeat("../", nestingDeep));
4551
var cssFileLink = prefix + "css/main.css";
@@ -52,7 +58,8 @@ public async Task WriteHtml(TextWriter writer, string title, string description,
5258
Title = title,
5359
Description = description,
5460
RenderedMarkdown = renderedMarkdown,
55-
CssLink = cssFileLink
61+
CssLink = cssFileLink,
62+
EditPageLink = editPageLink
5663
};
5764

5865
var renderedHtml = await _engine.CompileRenderAsync("Markdown.Renderer.Res.Html.main.cshtml", model);

Source/Markdown.Renderer/Res/Html/main.cshtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@
1313
</head>
1414
<body>
1515
@Raw(@Model.RenderedMarkdown)
16+
@if (!string.IsNullOrEmpty(Model.EditPageLink))
17+
{
18+
<hr/>
19+
<a href="@Model.EditPageLink">✎ Edit Page</a>
20+
}
1621
</body>
1722
</html>

Source/Markdown.Renderer/Res/Model/MainModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@ public class MainModel
55
public string Title { get; set; } = string.Empty;
66
public string Description { get; set; } = string.Empty;
77
public string RenderedMarkdown { get; set; } = string.Empty;
8+
9+
/// <summary>
10+
/// Hyperlink to the website that allows editing the current site
11+
/// </summary>
12+
public string EditPageLink { get; set; } = string.Empty;
813
public string CssLink { get; set; } = string.Empty;
914
}

Source/MarkdownUtil/Commands/RenderCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public override int Execute([NotNull] CommandContext context, [NotNull] RenderCo
3838
CopyAssets(rootPath, outPath, graph);
3939

4040
AnsiConsole.WriteLine($"Rendering {nodeCount} Files");
41-
RenderNodesToHtml(rootPath, outPath, graph);
41+
RenderNodesToHtml(rootPath, outPath, graph, settings.EditPageRoot);
4242

4343
return 0;
4444
}
4545

46-
private void RenderNodesToHtml(string rootPath, string outPath, MarkdownGraph graph)
46+
private void RenderNodesToHtml(string rootPath, string outPath, MarkdownGraph graph, string editPageRoot)
4747
{
48-
var rendererVisitor = new MarkdigVisitor(rootPath, outPath, _renderer);
48+
var rendererVisitor = new MarkdigVisitor(rootPath, outPath, _renderer, editPageRoot);
4949
graph.Visit(rendererVisitor);
5050
}
5151

Source/MarkdownUtil/Commands/Settings/RenderCommandSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public sealed class RenderCommandSettings : CommandSettings, IVisitorSettings
1313
[CommandOption("--hidden")]
1414
[DefaultValue(true)]
1515
public bool IncludeHidden { get; init; }
16+
17+
[CommandOption("--editPageRoot")]
18+
[Description("URL to the root url for editing the source files")]
19+
[DefaultValue("")]
20+
public string EditPageRoot { get; set; }
1621

1722
[Description("Root/Home Path for the Index")]
1823
[CommandArgument(0, "[PATH]")]

Source/MarkdownUtil/Service/Visitors/MarkdigVisitor.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,38 @@ namespace MarkdownUtil.Service.Visitors;
66

77
public class MarkdigVisitor : BaseVisitor
88
{
9-
public MarkdigVisitor(string rootPath, string outPath, HtmlRenderer htmlFoundation) : base(rootPath, outPath, ".html")
9+
public MarkdigVisitor(
10+
string rootPath,
11+
string outPath,
12+
HtmlRenderer htmlFoundation,
13+
string editPageRoot) : base(rootPath, outPath, ".html")
1014
{
1115
_htmlFoundation = htmlFoundation;
16+
_editPageRoot = editPageRoot;
1217
}
1318

1419
private readonly HtmlRenderer _htmlFoundation;
20+
private readonly string _editPageRoot;
1521

1622
protected override bool ProcessFile(MarkdownFile entity, int graphDepth, FilePathInfo io)
1723
{
1824
AnsiConsole.WriteLine($"{io.InputRelativeFilePathToRootDirectory} => {io.OutputRelativeFilePathToRootDirectory}");
1925

2026
var markdown = io.ReadAllText();
21-
io.CreateOutputDirectory();
22-
io.DeleteOutputFileIfExists();
27+
io.CreateOutputDirectory(); // ensures that the output directory exits
28+
io.DeleteOutputFileIfExists(); // deletes old output file if it exits
2329

30+
var editPageLink = string.IsNullOrEmpty(_editPageRoot) ? string.Empty : Path.Combine(_editPageRoot, io.InputRelativeFilePathToRootDirectory);
31+
2432
var outputFileName = io.OutputFullFilePath;
33+
2534
if (outputFileName.EndsWith("readme.html", StringComparison.InvariantCultureIgnoreCase))
2635
outputFileName = outputFileName.Substring(0, outputFileName.Length - 11) + "index.html";
36+
// Delete the Output file if it already exits
37+
if (File.Exists(outputFileName)) File.Delete(outputFileName);
2738
using var fs = new FileStream(outputFileName, FileMode.CreateNew, FileAccess.Write);
2839
using var sw = new StreamWriter(fs);
29-
_htmlFoundation.WriteHtml(sw, entity.Title, "", graphDepth, markdown).Wait();
40+
_htmlFoundation.WriteHtml(sw, entity.Title, "", graphDepth, markdown, editPageLink).Wait();
3041
return true;
3142
}
3243
}

0 commit comments

Comments
 (0)