forked from xoofx/markdig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mdtoc simple tool to generate a markdown TOC from a local markdow…
…n file or github link to a markdown file
- Loading branch information
Showing
5 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> | ||
</startup> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (c) Alexandre Mutel. All rights reserved. | ||
// This file is licensed under the BSD-Clause 2 license. | ||
// See the license.txt file in the project root for more information. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using Markdig; | ||
using Markdig.Extensions.AutoIdentifiers; | ||
using Markdig.Renderers; | ||
using Markdig.Renderers.Html; | ||
using Markdig.Syntax; | ||
|
||
namespace mdtoc | ||
{ | ||
/// <summary> | ||
/// A tool to generate a markdown TOC from a markdown local file or a github link to a markdown file. | ||
/// </summary> | ||
class Program | ||
{ | ||
static void Error(string message) | ||
{ | ||
Console.WriteLine(message); | ||
Environment.Exit(1); | ||
} | ||
|
||
static void Main(string[] args) | ||
{ | ||
if (args.Length != 1 || args[0] == "--help" || args[0] == "-help" || args[0] == "/?" || args[0] == "/help") | ||
{ | ||
Error("Usage: mdtoc [markdown file path | http github URL]"); | ||
return; | ||
} | ||
|
||
var path = args[0]; | ||
string markdown = null; | ||
if (path.StartsWith("https:")) | ||
{ | ||
Uri uri; | ||
if (!Uri.TryCreate(path, UriKind.Absolute, out uri)) | ||
{ | ||
Error($"Unable to parse Uri `{path}`"); | ||
return; | ||
} | ||
// Special handling of github URL to access the raw content instead | ||
if (uri.Host == "github.com") | ||
{ | ||
// https://github.com/lunet-io/scriban/blob/master/doc/language.md | ||
// https://raw.githubusercontent.com/lunet-io/scriban/master/doc/language.md | ||
var newPath = uri.AbsolutePath; | ||
var paths = new List<string>(newPath.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries)); | ||
if (paths.Count < 5 || paths[2] != "blob") | ||
{ | ||
Error($"Invalid github.com URL `{path}`"); | ||
return; | ||
} | ||
paths.RemoveAt(2); // remove blob | ||
uri = new Uri($"https://raw.githubusercontent.com/{(string.Join("/", paths))}"); | ||
} | ||
|
||
var httpClient = new HttpClient(); | ||
markdown = httpClient.GetStringAsync(uri).ConfigureAwait(false).GetAwaiter().GetResult(); | ||
} | ||
else | ||
{ | ||
markdown = File.ReadAllText(path); | ||
} | ||
|
||
var pipeline = new MarkdownPipelineBuilder().UseAutoIdentifiers(AutoIdentifierOptions.GitHub).Build(); | ||
var doc = Markdown.Parse(markdown, pipeline); | ||
|
||
// Precomputes the minHeading | ||
var headings = doc.Descendants<HeadingBlock>().ToList(); | ||
int minHeading = int.MaxValue; | ||
int maxHeading = int.MinValue; | ||
foreach (var heading in headings) | ||
{ | ||
minHeading = Math.Min(minHeading, heading.Level); | ||
maxHeading = Math.Max(maxHeading, heading.Level); | ||
} | ||
|
||
var writer = Console.Out; | ||
// Use this htmlWriter to write content of headings into link label | ||
var htmlWriter = new HtmlRenderer(writer) {EnableHtmlForInline = true}; | ||
foreach (var heading in headings) | ||
{ | ||
var indent = heading.Level - minHeading; | ||
for (int i = 0; i < indent; i++) | ||
{ | ||
// - Start Of Heading | ||
writer.Write(" "); | ||
} | ||
writer.Write("- ["); | ||
htmlWriter.WriteLeafInline(heading); | ||
writer.Write($"](#{heading.GetAttributes().Id})"); | ||
writer.WriteLine(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Alexandre Mutel. All rights reserved. | ||
// This file is licensed under the BSD-Clause 2 license. | ||
// See the license.txt file in the project root for more information. | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("mdtoc")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("Alexandre Mutel")] | ||
[assembly: AssemblyProduct("mdtoc")] | ||
[assembly: AssemblyCopyright("Copyright © 2017 - Alexandre Mutel")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("e3cdff0f-5bfc-42e9-bdba-2797651900a2")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// 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("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{E3CDFF0F-5BFC-42E9-BDBA-2797651900A2}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>mdtoc</RootNamespace> | ||
<AssemblyName>mdtoc</AssemblyName> | ||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Markdig\Markdig.csproj"> | ||
<Project>{8a58a7e2-627c-4f41-933f-5ac92adfab48}</Project> | ||
<Name>Markdig</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |