Skip to content

Commit a99897f

Browse files
authored
Merge pull request #13 from RoboKiwi/feature/monaco-code-blocks
Syntax highlighting using Monaco editor
2 parents 5196534 + 9d40073 commit a99897f

File tree

21 files changed

+327
-128
lines changed

21 files changed

+327
-128
lines changed

.vscode/dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Custom Dictionary Words
2+
RoboKiwi

.vscode/settings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"cSpell.customDictionaries": {
3+
"custom-dictionary-workspace": {
4+
"name": "custom-dictionary-workspace",
5+
"path": "${workspaceFolder:SiteGen}/.vscode/dictionary.txt",
6+
"addWords": true,
7+
"scope": "workspace"
8+
}
9+
}
10+
}

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ SiteGen is a .NET-based static site generator.
77
## Features
88

99
- Markdown support
10-
- Pygments syntax highlighting
10+
- Syntax Highlighting
11+
- Pygments
12+
- Prism.js
13+
- Monaco
1114
- Mermaid diagrams
1215
- Razor templates
1316
- Rich site hierarchy model, for building navigation in your templates
1417
- Table of Contents
1518
- Extensible
1619

17-
## Usage
20+
## Get Started
1821

1922
In your ASP.NET website, reference `SiteGen.Core`.
2023

@@ -51,7 +54,18 @@ app.UseSiteGen();
5154
app.Run();
5255
```
5356

54-
Add your `.md` files in a `content` folder, and your static assets in `static`.
57+
Configure paths in `appsettings.json`:
58+
59+
```json
60+
{
61+
"ContentPaths": [ "content" ],
62+
"StaticPaths": [ "static", "wwwroot" ]
63+
}
64+
```
65+
66+
Add your `.md` files into the `content` folder, and your static assets in `static` or `wwwroot`.
67+
68+
Add a `Page.cshtml` to 'Pages/Shared' (for Razor Pages) or 'Views/Home' or 'Views/Shared' (for MVC) folder.
5569

5670
If you generate client-side css and javascript, you can distribute your built artifacts to either the `static` or `wwwroot` folders.
5771

@@ -69,6 +83,14 @@ dotnet tool install --local SiteGen.Cli
6983
- Launch your website and bind to port :5000
7084
- Run `dotnet sitegen` to crawl your site and publish the static resources to `/public`
7185

86+
# How it works
87+
88+
SiteGen registers itself as a default fallback route. This allows your website to implement its own routes which will take precedence.
89+
90+
By default, SiteGen will generate a site map from the markdown files it finds in the configured content directories.
91+
92+
The SiteGen fallback route it for its in-built HomeController and Page action. This is why you must implement a default Page.cshtml view.
93+
7294
# Services
7395

7496
# Interactive, hosted browser scraping with Playwright

SiteGen.sln

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SiteGen.Extensions.Markdown
1515
EndProject
1616
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
1717
ProjectSection(SolutionItems) = preProject
18-
LICENSE.txt = LICENSE.txt
19-
README.md = README.md
18+
docs\syntax-highlighting.md = docs\syntax-highlighting.md
2019
EndProjectSection
2120
EndProject
2221
Global

docs/syntax-highlighting.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Syntax highlighting
2+
3+
Syntax highlighting is applied to fenced code blocks in Markdown files.
4+
5+
````markdown
6+
```javascript
7+
console.log("Hello, world!");
8+
```
9+
````
10+
11+
You can also apply additional arguments to the fenced code block:
12+
13+
## Line numbers
14+
15+
Enable or disable line numbers
16+
17+
## Line highlighting
18+
19+
Highlight specific lines in the code block
20+
21+
## Theme
22+
23+
Override the default theme for the code block
24+
25+
## Highlighting engine
26+
27+
Configure the specific syntax highlighting engine to use for the code block
28+
29+
# Themes and styles
30+
31+
Your site must bundle (recommended) or dynamically the CSS for the syntax highlighting engine you choose.
32+
33+
You can extract the styles using the SiteGen CLI tool.
34+
35+
Try to include both dark and light themes, and use the `prefers-color-scheme` media query to switch between them.
36+
37+
# Supported syntax highlighting engines
38+
39+
Be careful configuring more than one syntax highlighting engine, as they will conflict with each other.
40+
41+
## Pygments
42+
43+
Pygments is written in Python.
44+
45+
You need to have the `pygmentize` command available on your system to use it.
46+
47+
TODO: Rewrite to use `pygments` directly via isolated Python environment.
48+
49+
## Prism.js
50+
51+
```csharp
52+
services.ConfigurePrism();
53+
```
54+
55+
[Prism.js](https://prismjs.com/)
56+
57+
## Monaco Editor
58+
59+
```csharp
60+
services.ConfigureMonaco();
61+
```
62+
63+
The [Monaco Editor](https://microsoft.github.io/monaco-editor/) is the code editor that powers Visual Studio Code.
64+
65+
### Themes
66+
67+
- `vs` Visual Studio Light (default)
68+
- `vs-dark` Visual Studio Dark
69+
- `hc-black` High Contrast Black
70+
- `hc-light` High Contrast Light
71+
72+
## Pending
73+
74+
- [Highlight.js](https://highlightjs.org/)

src/SiteGen.Cli/Command.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ enum Command
44
{
55
None = 0,
66
Build = 1,
7-
Serve = 2
7+
Serve = 2,
8+
ExportCss = 4
89
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.Extensions.Logging;
2+
using SiteGen.Core;
3+
4+
namespace SiteGen.Cli.Commands;
5+
6+
class SyntaxHighlightingCss : ICommandlet
7+
{
8+
private readonly IMonacoService monaco;
9+
private readonly ILogger<SyntaxHighlightingCss> logger;
10+
11+
public SyntaxHighlightingCss(IMonacoService monaco, ILogger<SyntaxHighlightingCss> logger)
12+
{
13+
this.monaco = monaco;
14+
this.logger = logger;
15+
}
16+
17+
public async Task ExecuteAsync()
18+
{
19+
logger.LogInformation("Generating syntax highlighting CSS...");
20+
var css = await monaco.GetCssAsync("vs-dark");
21+
Console.WriteLine(css);
22+
}
23+
}

src/SiteGen.Cli/Program.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using SiteGen.Cli;
55
using SiteGen.Cli.Commands;
66
using System.Diagnostics;
7+
using SiteGen.Core;
8+
using Microsoft.Extensions.Logging;
79

810
var configBuilder = new ConfigurationBuilder();
911
configBuilder.AddEnvironmentVariables();
@@ -25,9 +27,19 @@
2527
configuration.Bind(settings);
2628

2729
var services = new ServiceCollection();
30+
31+
services.AddLogging(builder => {
32+
builder.AddConsole().SetMinimumLevel(LogLevel.Information);
33+
});
34+
2835
services.AddSingleton(configuration);
2936
services.AddSingleton(settings);
3037
services.AddTransient<ServeCommand>();
38+
services.AddTransient<SyntaxHighlightingCss>();
39+
40+
services.ConfigurePlaywright();
41+
42+
services.AddMonaco();
3143

3244
var serviceProviderOptions = new ServiceProviderOptions { ValidateScopes = true, ValidateOnBuild = true };
3345
var factory = new DefaultServiceProviderFactory(serviceProviderOptions);
@@ -44,6 +56,9 @@
4456
case Command.Serve:
4557
command = serviceProvider.GetRequiredService<ServeCommand>();
4658
break;
59+
case Command.ExportCss:
60+
command = serviceProvider.GetRequiredService<SyntaxHighlightingCss>();
61+
break;
4762
default:
4863
break;
4964
}

src/SiteGen.Cli/SiteGen.Cli.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<ItemGroup>
1717
<ProjectReference Include="..\SiteGen.Core\SiteGen.Core.csproj" />
18+
<ProjectReference Include="..\SiteGen.Extensions.Markdown.Monaco\SiteGen.Extensions.Markdown.Monaco.csproj" />
1819
</ItemGroup>
1920

2021
</Project>

src/SiteGen.Core/Extensions/Markdown/Pygments/PygmentsMarkdownExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public void Setup(MarkdownPipelineBuilder pipeline)
99
{
1010
if (pipeline.BlockParsers.Contains<PygmentsBlockParser>()) return;
1111

12-
// High precendence so we can render before syntax highlighting
12+
// High precedence so we can render before syntax highlighting
1313
pipeline.BlockParsers.Insert(0, new PygmentsBlockParser());
1414
}
1515

0 commit comments

Comments
 (0)