diff --git a/src/DocNet/Config.cs b/src/DocNet/Config.cs
index 73d5931..2ff4195 100644
--- a/src/DocNet/Config.cs
+++ b/src/DocNet/Config.cs
@@ -207,7 +207,15 @@ namespace Docnet
return string.IsNullOrWhiteSpace(rawIncludeFolder) ? "Includes" : rawIncludeFolder;
}
}
-
+
+ public bool ConvertLocalLinks
+ {
+ get
+ {
+ return _configData.ConvertLocalLinks ?? false;
+ }
+ }
+
public string ThemeName
{
get
diff --git a/src/DocNet/SimpleNavigationElement.cs b/src/DocNet/SimpleNavigationElement.cs
index db667de..f905766 100644
--- a/src/DocNet/SimpleNavigationElement.cs
+++ b/src/DocNet/SimpleNavigationElement.cs
@@ -68,7 +68,7 @@ namespace Docnet
this.MarkdownFromFile = File.ReadAllText(sourceFile, Encoding.UTF8);
// Check if the content contains @@include tag
content = Utils.IncludeProcessor(this.MarkdownFromFile, Utils.MakeAbsolutePath(activeConfig.Source, activeConfig.IncludeFolder));
- content = Utils.ConvertMarkdownToHtml(content, Path.GetDirectoryName(destinationFile), activeConfig.Destination, sourceFile, _relativeH2LinksOnPage);
+ content = Utils.ConvertMarkdownToHtml(content, Path.GetDirectoryName(destinationFile), activeConfig.Destination, sourceFile, _relativeH2LinksOnPage, activeConfig.ConvertLocalLinks);
}
else
{
@@ -90,7 +90,7 @@ namespace Docnet
defaultMarkdown.AppendFormat("* [{0}]({1}{2}){3}", sibling.Name, relativePathToRoot, HttpUtility.UrlPathEncode(sibling.TargetURL), Environment.NewLine);
}
defaultMarkdown.Append(Environment.NewLine);
- content = Utils.ConvertMarkdownToHtml(defaultMarkdown.ToString(), Path.GetDirectoryName(destinationFile), activeConfig.Destination, string.Empty, _relativeH2LinksOnPage);
+ content = Utils.ConvertMarkdownToHtml(defaultMarkdown.ToString(), Path.GetDirectoryName(destinationFile), activeConfig.Destination, string.Empty, _relativeH2LinksOnPage, activeConfig.ConvertLocalLinks);
}
else
{
diff --git a/src/DocNet/Utils.cs b/src/DocNet/Utils.cs
index 5a53822..d5becc3 100644
--- a/src/DocNet/Utils.cs
+++ b/src/DocNet/Utils.cs
@@ -37,19 +37,20 @@ namespace Docnet
/// Regex expression used to parse @@include(filename.html) tag.
///
private static Regex includeRegex = new Regex(@"@@include\((.*)\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
- #endregion
-
- ///
- /// Converts the markdown to HTML.
- ///
- /// The markdown string to convert.
- /// The document path (without the document filename).
- /// The site root.
- /// the filename of the source markdown file
- /// The created anchor collector, for ToC sublinks for H2 headers.
- ///
- public static string ConvertMarkdownToHtml(string toConvert, string destinationDocumentPath, string siteRoot, string sourceDocumentFilename,
- List> createdAnchorCollector)
+ #endregion
+
+ ///
+ /// Converts the markdown to HTML.
+ ///
+ /// The markdown string to convert.
+ /// The document path (without the document filename).
+ /// The site root.
+ /// the filename of the source markdown file
+ /// The created anchor collector, for ToC sublinks for H2 headers.
+ /// if set to true, convert local links to md files to target files.
+ ///
+ public static string ConvertMarkdownToHtml(string toConvert, string destinationDocumentPath, string siteRoot, string sourceDocumentFilename,
+ List> createdAnchorCollector, bool convertLocalLinks)
{
var parser = new MarkdownDeep.Markdown
{
@@ -58,7 +59,8 @@ namespace Docnet
AutoHeadingIDs = true,
NewWindowForExternalLinks = true,
DocNetMode = true,
- DestinationDocumentLocation = destinationDocumentPath,
+ ConvertLocalLinks = convertLocalLinks,
+ DestinationDocumentLocation = destinationDocumentPath,
DocumentRoot = siteRoot,
SourceDocumentFilename = sourceDocumentFilename,
HtmlClassTitledImages = "figure",
diff --git a/src/MarkdownDeep/LinkDefinition.cs b/src/MarkdownDeep/LinkDefinition.cs
index 9f1f5ef..487b8e7 100644
--- a/src/MarkdownDeep/LinkDefinition.cs
+++ b/src/MarkdownDeep/LinkDefinition.cs
@@ -58,9 +58,36 @@ namespace MarkdownDeep
{
HtmlTag tag = new HtmlTag("a");
+ var url = this.Url;
+
+ if (m.DocNetMode && m.ConvertLocalLinks)
+ {
+ // A few requirements before we can convert local links:
+ // 1. Link contains .md
+ // 2. Link is relative
+ // 3. Link is included in the index
+ var index = url.LastIndexOf(".md", StringComparison.OrdinalIgnoreCase);
+ if (index >= 0)
+ {
+ Uri uri;
+ if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uri))
+ {
+ if (!uri.IsAbsoluteUri)
+ {
+ // TODO: Check if link exists in the ToC
+ var existsInTableOfContents = true;
+ if (existsInTableOfContents)
+ {
+ url = url.Remove(index, ".md".Length).Insert(index, ".htm");
+ }
+ }
+ }
+ }
+ }
+
// encode url
StringBuilder sb = m.GetStringBuilder();
- Utils.SmartHtmlEncodeAmpsAndAngles(sb, this.Url);
+ Utils.SmartHtmlEncodeAmpsAndAngles(sb, url);
tag.attributes["href"] = sb.ToString();
// encode title
diff --git a/src/MarkdownDeep/MardownDeep.cs b/src/MarkdownDeep/MardownDeep.cs
index 0352cec..9b496fe 100644
--- a/src/MarkdownDeep/MardownDeep.cs
+++ b/src/MarkdownDeep/MardownDeep.cs
@@ -846,10 +846,16 @@ namespace MarkdownDeep
///
public bool DocNetMode { get; set; }
- // When set, all html block level elements automatically support
- // markdown syntax within them.
- // (Similar to Pandoc's handling of markdown in html)
- public bool MarkdownInHtml
+
+ ///
+ /// Gets or sets a value indicating whether local links to markdown files should be converted as well.
+ ///
+ public bool ConvertLocalLinks { get; set; }
+
+ // When set, all html block level elements automatically support
+ // markdown syntax within them.
+ // (Similar to Pandoc's handling of markdown in html)
+ public bool MarkdownInHtml
{
get;
set;
diff --git a/src/MarkdownDeepTests/LocalLinkTests.cs b/src/MarkdownDeepTests/LocalLinkTests.cs
new file mode 100644
index 0000000..e4716d8
--- /dev/null
+++ b/src/MarkdownDeepTests/LocalLinkTests.cs
@@ -0,0 +1,33 @@
+using System.Collections.Generic;
+using NUnit.Framework;
+
+namespace MarkdownDeepTests
+{
+ [TestFixture]
+ class LocalLinkTests
+ {
+ public static IEnumerable GetTests_locallinks_enabled()
+ {
+ return Utils.GetTests("locallinks_enabled");
+ }
+
+
+ [Test, TestCaseSource("GetTests_locallinks_enabled")]
+ public void Test_locallinks_enabled(string resourceName)
+ {
+ Utils.RunResourceTest(resourceName);
+ }
+
+ public static IEnumerable GetTests_locallinks_disabled()
+ {
+ return Utils.GetTests("locallinks_disabled");
+ }
+
+
+ [Test, TestCaseSource("GetTests_locallinks_disabled")]
+ public void Test_locallinks_disabled(string resourceName)
+ {
+ Utils.RunResourceTest(resourceName);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/MarkdownDeepTests/MarkdownDeepTests.csproj b/src/MarkdownDeepTests/MarkdownDeepTests.csproj
index 0d2b8f2..21def54 100644
--- a/src/MarkdownDeepTests/MarkdownDeepTests.csproj
+++ b/src/MarkdownDeepTests/MarkdownDeepTests.csproj
@@ -76,6 +76,7 @@
+
@@ -401,6 +402,12 @@
+
+
+
+
+
+
@@ -487,6 +494,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+