Skip to content

Commit

Permalink
Add Support for loading Markdown Island content from file in `<ww:Mar…
Browse files Browse the repository at this point in the history
…kdown>` control via `Filename` property.
  • Loading branch information
RickStrahl committed Nov 17, 2018
1 parent 718139f commit 48d03de
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 86 deletions.
10 changes: 8 additions & 2 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Westwind.Web.Markdown Changelog

## Version 0.2.8
* October 30th, 2018*
## Version 0.2.9
*November 20th, 2018*

* **Add File Loading to Markdown Control**
Added a `Filename` property to the Markdown control that allows specifying a virtual path to a Markdown document (such as `<ww:markdown Filename="~/EmbeddedMarkdownFile.md"`). This allows for easy referencing of large Markdown blocks without having to type them directly into the host document.

### Version 0.2.8
*October 30th, 2018*

* **Add MarkdownPageHandler to render Markdown Documents from Disk**
Created a new Markdown handler that can serve Markdown pages from disk and use a template to render the HTML content as a complete page. The idea is to allow you to drop Markdown files into any Web site and render them as full HTML documents using your site's chrome.
Expand Down
16 changes: 10 additions & 6 deletions Nuget/Westwind.Web.Markdown.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<package xmlns="http://schemas.microsoft.com/packaging/2013/01/nuspec.xsd">
<metadata minClientVersion="2.25">
<id>Westwind.Web.Markdown</id>
<version>0.2.8</version>
<title>West Wind Web Markdown Processing</title>
<version>0.2.9</version>
<title>West Wind Web Markdown Processing Tools</title>
<authors>Rick Strahl,West Wind Technologies</authors>
<licenseUrl>https://github.com/RickStrahl/Westwind.Web.Markdown</licenseUrl>
<projectUrl>https://github.com/RickStrahl/Westwind.Web.Markdown</projectUrl>
Expand All @@ -15,16 +15,20 @@
Provides the following features:

* Markdown.Parse(), Markdown.ParseHtml() methods
* A Markdown Control for static Markdown text or dynamic assignment
* Can render loose Markdown files using a customizable template
* Markdown Islands Markdown Control
* A Markdown WebForms Control for static Markdown text or dynamic assignment
* Load static text
* Load content from files
* Databind text
* Loose Markdown file support - drop Markdown files into your site
* Supports HTML Sanitation
* Supports White Space handling for Markdown text
* Customization of Markdown Parser
* Supports many Markdown support features courtesy of MarkDig

For .NET Core support see `Westwind.AspNetCore.Markdown`.
For .NET Core support with similar features see `Westwind.AspNetCore.Markdown`.
</description>
<summary>Markdown Parsing support for System.Web based ASP.NET Applications. Includes Markdown Parsing helpers and a WebForms Control along with a number of support features.</summary>
<summary>Markdown Processing for System.Web based ASP.NET WebForms and MVC Applications</summary>
<releaseNotes></releaseNotes>
<copyright>Rick Strahl, West Wind Technologies 2017-2018</copyright>
<language>en-US</language>
Expand Down
27 changes: 0 additions & 27 deletions Nuget/Westwind.Web.MarkdownControl.nuspec

This file was deleted.

Binary file modified Nuget/lib/net40/Westwind.Web.Markdown.dll
Binary file not shown.
10 changes: 10 additions & 0 deletions Nuget/lib/net40/Westwind.Web.Markdown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions WebSample/Default.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<form id="form1" runat="server" >
<div>

<h3 class="header" >Markdown with Normalized Text (default, doesn't have to be left-aligned):</h3>
<h2 class="header" >Markdown with Normalized Text (default, doesn't have to be left-aligned):</h2>


<div class="small margin-bottom-2x margin-top" style="color: #bbb">rendered Markdown starts below...</div>
Expand Down Expand Up @@ -51,10 +51,8 @@
</ww:Markdown>


<h3 class="header">Markdown Display (non-normalized, has to be left aligned):</h3>
<h2 class="header">Markdown Display (non-normalized, has to be left aligned):</h2>

<hr />


<ww:Markdown runat="server" id="md1" NormalizeWhiteSpace="false">
### Eeeer, hold on there Doc: ASP.NET Core on IIS
Expand Down Expand Up @@ -97,8 +95,8 @@ public ActionResult LetsEncrypt(string id)
</ww:Markdown>


<h3 class="header">SanitizeHtml Option</h3>
<hr />
<h2 class="header">SanitizeHtml Option</h2>


<ww:Markdown runat="server" SanitizeHtml="True">
<h5>There's a script hiding below (shouldn't fire alert box on load!)</h5>
Expand All @@ -115,8 +113,8 @@ public ActionResult LetsEncrypt(string id)

</ww:Markdown>

<h3 class="header">Dynamically assign Markdown:</h3>
<hr />
<h2 class="header">Dynamically assigned Markdown:</h2>


<label class="">Enter some Markdown:</label><br />
<asp:TextBox runat="server" ID="EditedMarkdown" TextMode="Multiline" class="form-control" style="height: 245px;">#### Markdown Text
Expand All @@ -132,11 +130,19 @@ Write some markdown text and see if it sticks.
alert("GOTCHA: You shouldn't see this script with SanitizeHtml on");
</script>
</asp:TextBox>

<asp:Button runat="server" ID="btnSubmit" Text="Save" />
<hr />

<%= Markdown.Parse(EditedMarkdown.Text, sanitizeHtml: true) %>



<h2 class="header">Markdown File Embedding</h2>
<ww:Markdown runat="server" Filename="./MarkdownFileEmbedding.md" >
</ww:Markdown>


</div>


Expand Down
15 changes: 0 additions & 15 deletions WebSample/Default2.aspx

This file was deleted.

14 changes: 0 additions & 14 deletions WebSample/Default2.aspx.cs

This file was deleted.

10 changes: 10 additions & 0 deletions WebSample/MarkdownFileEmbedding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

---

### Embedded Markdown from File
This is embedded Markdown that is loaded into the page from an external Markdown file (`~/EmbeddedMarkdownFile.md`) using the `Filename` property on the `<markdown>` control.

This allows you to edit markdown in an external Markdown editor and keep it without having to paste the Markdown into a Markdown control.

---

7 changes: 7 additions & 0 deletions WebSample/MasterPage.master
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
<link href="<%= ResolveUrl("~/") %>lib/fontawesome/css/all.min.css" rel="stylesheet" />
<link href="<%= ResolveUrl("~/") %>css/application.css" rel="stylesheet" />

<style>
.header {
margin-top: 40px;
border-bottom: 1px #ddd solid;
padding-bottom: 0.4em;
}
</style>
<asp:ContentPlaceHolder id="headers" runat="server">
</asp:ContentPlaceHolder>
</head>
Expand Down
2 changes: 1 addition & 1 deletion WebSample/css/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ h2 {
color: #444;
}
h3 {
font-size: 1.25em;
font-size: 1.35em;
color: #2B547E;
}
h4 {
Expand Down
44 changes: 40 additions & 4 deletions Westwind.Web.Markdown/Markdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@


using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
Expand Down Expand Up @@ -66,16 +67,51 @@ public class Markdown : System.Web.UI.WebControls.Literal

public bool SanitizeHtml { get; set; } = true;


/// <summary>
/// Virtual site relative file path that is loaded and displayed in the control's
/// content.
///
/// ~/Markdown/Site
///
/// Note related resources inside of the Markdown are *host page relative*, not Markdown document relative.
/// </summary>
[Description("Optional file name from which to load the Markdown. Overrides content setting if set. Note: markdown related resources are *HTML host page relative* not relative to the markdown document.")]
[Category("Markdown")]
public string Filename { get; set; } = null;

/// <summary>
/// Overrides the HTML rendering of content
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
if (string.IsNullOrEmpty(Text))
return;

string markdown = NormalizeWhiteSpaceText(Text);
string markdown = null;

if (!string.IsNullOrEmpty(Filename))
{
var file = Context.Server.MapPath(Filename);
try
{
markdown = File.ReadAllText(file);
}
catch
{
return;
}
}
else
{

if (string.IsNullOrEmpty(Text))
return;

if (NormalizeWhiteSpace)
markdown = NormalizeWhiteSpaceText(Text);
else
markdown = Text;
}

var parser = MarkdownParserFactory.GetParser(false, false);
var html = parser.Parse(markdown,SanitizeHtml);
Expand Down Expand Up @@ -164,7 +200,7 @@ public static string Parse(string markdown, bool usePragmaLines = false, bool fo
}

/// <summary>
/// Renders raw Markdown from string to HTML.
/// Renders raw Markdown from string to HTML with an HTMLString output for use in MVC or Web Pages instead of Raw output.
/// </summary>
/// <param name="markdown">Markdown to render</param>
/// <param name="usePragmaLines">Generates line numbers as ids into headers and paragraphs. Useful for previewers to match line numbers to rendered output</param>
Expand Down
4 changes: 1 addition & 3 deletions Westwind.Web.Markdown/MarkdownHandler/MarkdownBasePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ public class MarkdownBasePage : Page
public MarkdownModel MarkdownModel { get; set; }

protected override void OnInit(EventArgs e)
{

{
base.OnInit(e);
MarkdownModel = Context.Items[MarkdownHttpHandler.ModelKey] as MarkdownModel;
}

}

public class MarkdownModel
Expand Down
2 changes: 1 addition & 1 deletion Westwind.Web.Markdown/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.8")]
[assembly: AssemblyVersion("0.2.9")]

5 changes: 2 additions & 3 deletions Westwind.Web.Markdown/Westwind.Web.Markdown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Markdig, Version=0.15.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Markdig.0.15.2\lib\net40\Markdig.dll</HintPath>
<Reference Include="Markdig, Version=0.15.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Markdig.0.15.4\lib\net40\Markdig.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
Expand Down Expand Up @@ -64,6 +64,5 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
2 changes: 1 addition & 1 deletion Westwind.Web.Markdown/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Markdig" version="0.15.2" targetFramework="net40" />
<package id="Markdig" version="0.15.4" targetFramework="net40" />
</packages>
17 changes: 16 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This library provides:
Simple Markdown Parsing with `Markdown.Parse()` and `Markdown.ParseHtml()` methods you can use in code or in your MVC or WebForms markup.

* **Markdown WebForms Control for 'Markdown Islands'**
A useful `<markdown>` server WebControl for easily embedding static Markdown text or data-bound Markdown into a document. Great for using Markdown for static content inside of larger Web Pages.
A useful `<ww:markdown>` server WebControl for easily embedding static Markdown text, Markdown from a file or data-bound Markdown into a document. Great for using Markdown for static content inside of larger Web Pages including the ability to load content from file.

* **Serve Markdown Pages from Disk using MarkdownHttpHandler**
This Http handler allows you to drop Markdown files into a site and serve those Markdown files as HTML rendered into a configurable template. The template's job is to provide the site chrome around the HTML so this can simply be a small content page pointing a Master Page in WebForms or Layout Page in MVC. A model object is passed via the Items collection.
Expand Down Expand Up @@ -97,6 +97,7 @@ Then embed the control into the page where you want the markdown to appear:
```xml
<ww:Markdown runat="server" id="md2"
NormalizeWhiteSpace="True"
Filename=""
SanitizeHtml="True">
# Markdown Monster Change Log
[download latest version](https://markdownmonster.west-wind.com/download.aspx) &bull;
Expand Down Expand Up @@ -172,6 +173,20 @@ Both `Markdown.Parse()` and `Markdown.ParseHtml()` also have a sanitizeHtml para

If you render static text you control then `SanitizeHtml=False` is usually Ok, but if you take user input and put into the browser to display, **always use `SanitizeHtml=True`**.

#### Filename
You can optionally provide a file name in lieu of text content in the Markdown control body. Files can be specified as virtual paths:

```html
<ww:Markdown Filename="~/EmbeddedMarkdown.md"></ww:Markdown>
```

The file name is resolved and loaded from disk, converted into Markdown and rendered into the page.

The file name is fed to `Server.MapPath()` so any path that is valid for `MapPath()` will work, but for portability I recommend you either use a page relative path or a virtual path as shown above.

> #### Related Resources are Host Page relative
> Please not that resources linked in the Markdown document - images mostly - are **host page** relative, not relative to the Markdown file. In most cases it'll be best to place referenced Markdown documents in the same folder as the host page to ensure that relative links work consistently.
### Static Markdown Rendering
The control also includes static Markdown rendering that you can use in your Web Application inside of pages or your Web code.

Expand Down

0 comments on commit 48d03de

Please sign in to comment.