8000 Added an option to use line-numbers plugin for PrismJS by retrokiller543 · Pull Request #9 · ilich/Markdig.Prism · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added an option to use line-numbers plugin for PrismJS #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 70 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Markdig.Prism
An extension that adds syntax highlighting to a [Markdig](https://github.com/lunet-io/markdig) pipeline through [Prism.js](https://prismjs.com/) JavaScript library.
# Markdig.Prism (Forked Version)

This is a forked version of the [Markdig.Prism](https://github.com/WebStoating/Markdig.Prism) library, an extension that adds syntax highlighting to a [Markdig](https://github.com/lunet-io/markdig) pipeline through [Prism.js](https://prismjs.com/) JavaScript library.

This forked version includes additional features such as a download button for code blocks. Please note that some of these features require additional JavaScript to function correctly.

## Usage

1. Add ```WebStoating.Markdig.Prism``` to your project.
1. Add this forked version of `Markdig.Prism` to your project.

```powershell
dotnet add package WebStoating.Markdig.Prism --version 1.0.0
dotnet add package WebStoating.Markdig.PrismWithPlugins --version 1.0.0
```

2. Create Markdig pipeline
Expand All @@ -18,7 +21,7 @@ private static readonly MarkdownPipeline MarkdownPipeline = new MarkdownPipeline
.Build();
```

3. Download [Prism](https://prismjs.com/download.html) and add it to your Razor page or layout template.
3. Download [Prism](https://prismjs.com/download.html) and add it to your Razor page or layout template. Make sure to include the additional JavaScript required for the plugins.

```html
<!DOCTYPE html>
Expand All @@ -30,6 +33,8 @@ private static readonly MarkdownPipeline MarkdownPipeline = new MarkdownPipeline
<body>
...
<script src="prism.js"></script>
<!--This would be where to put the aditiional javascript if needed for the plugin you need-->
<script src="yourAdditionalScript.js"></script>
</body>
</html>
```
Expand All @@ -38,4 +43,63 @@ private static readonly MarkdownPipeline MarkdownPipeline = new MarkdownPipeline

```csharp
var html = Markdown.ToHtml(markdown, MarkdownPipeline);
```
```

## Configuration

You can configure the extension by passing a `PrismExtensionOptions` object to the `UsePrism` method.

```csharp
private static readonly MarkdownPipeline MarkdownPipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UsePrism(new PrismExtensionOptions
{
UseDownloadButton = true,
UseLineNumbers = true,
})
.Build();
```

It will default to the following options:

```csharp
new PrismExtensionOptions
{
UseDownloadButton = false,
UseLineNumbers = false,
}
```
## Supported Plugins

| Supported | Plugin Name | Usage Guide |
| ----------- | --------- | ----------- |
| ❌ | Line Highlight | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/LineHighlightUsage.md) |
| ✅ | Line Numbers | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/LineNumbersUsage.md) |
| ❌ | Show Invisibles | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/ShowInvisiblesUsage.md) |
| ❌ | Autolinker | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/AutolinkerUsage.md) |
| ❌ | WebPlatform Docs | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/WebPlatformDocsUsage.md) |
| ❌ | Custom Class | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/CustomClassUsage.md) |
| ❌ | File Highlight | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/FileHighlightUsage.md) |
| ❌ | Show Language | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/ShowLanguageUsage.md) |
| ❌ | JSONP Highlight | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/JSONPHighlightUsage.md) |
| ❌ | Highlight Keywords | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/HighlightKeywordsUsage.md) |
| ❌ | Remove initial line feed | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/RemoveInitialLineFeedUsage.md) |
| ❌ | Inline color | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/InlineColorUsage.md) |
| ❌ | Previewers | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/PreviewersUsage.md) |
| ❌ | Autoloader | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/AutoloaderUsage.md) |
| ❌ | Keep Markup | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/KeepMarkupUsage.md) |
| ❌ | Command Line | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/CommandLineUsage.md) |
| ❌ | Unescaped Markup | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/UnescapedMarkupUsage.md) |
| ❌ | Normalize Whitespace | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/NormalizeWhitespaceUsage.md) |
| ❌ | Data-URI Highlight | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/DataURIHighlightUsage.md) |
| ✅ | Toolbar | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/ToolbarUsage.md) |
| ✅ | Copy to Clipboard Button | [Guide](./CopyToClipboardButtonUsage.md) |
| ✅ | Download Button | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/DownloadButtonUsage.md) |
| ❌ | Match braces | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/MatchBracesUsage.md) |
| ❌ | Diff Highlight | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/DiffHighlightUsage.md) |
| ❌ | Filter highlightAll | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/FilterHighlightAllUsage.md) |
| ❌ | Treeview | [Guide](https://github.com/Retrokiller543/Markdig.Prism/blob/main/docs/TreeviewUsage.md) |

## Credits

This library is a fork of [Markdig.Prism](https://github.com/WebStoating/Markdig.Prism) by [Ilya Verbitskiy](https://github.com/ilich). All credit for the original work goes to them.
88 changes: 88 additions & 0 deletions docs/DownloadButtonUsage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Download Button Usage Guide

The Download Button plugin allows users to download the content of a code block as a text file. This guide will show you how to use it.

## Prerequisites

To use the Download Button plugin, you need to include the following JavaScript code in your project:

```javascript
// This function generates a downloadable file from a string of text.
function downloadFile(filename, text, extension) {
var blob = new Blob([text], { type: "text/plain" });
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = url;
a.download = filename + extension;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}

// This function adds a click event listener to a download button.
function addDownloadButtonListener(id) {
var button = document.getElementById(id + "-download");
var codeBlock = document.getElementById(id);
var toolbar = codeBlock.parentElement.parentElement.lastElementChild;

var newDiv = document.createElement("div");
newDiv.className = "toolbar-item";
newDiv.appendChild(button);

toolbar.appendChild(newDiv);

button.addEventListener("click", function () {
var text = codeBlock.textContent;
var extension = button.getAttribute("data-extension") || ".txt";
downloadFile(id, text, extension);
});
}

// This function registers a button with the Prism toolbar plugin.
function registerDownloadButton() {
Prism.plugins.toolbar.registerButton('download-button', function (env) {
var button = document.createElement('button');
button.textContent = 'Download';

var pre = env.element.parentNode;
var id = pre.getAttribute('id');
var extension = pre.getAttribute('data-extension') || '.txt';

button.addEventListener('click', function () {
var code = env.element.textContent;
downloadFile(id, code, extension);
});

return button;
});
}

// This function adds download button listeners for each code block.
function addDownloadButtonListeners() {
var codeBlocks = document.querySelectorAll("pre[data-download-link]");
for (var i = 0; i < codeBlocks.length; i++) {
var codeBlock = codeBlocks[i].children[0];
addDownloadButtonListener(codeBlock.id);
}
}

// Run everything after the page has loaded.
window. () {
registerDownloadButton();
addDownloadButtonListeners();
};
```

## Usage
Set the `UseDownloadButton` option to `true` when configuring the extension.
```csharp
private static readonly MarkdownPipeline MarkdownPipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UsePrism(new PrismExtensionOptions
{
UseDownloadButton = true,
})
.Build();
```
Once the JavaScript code is included in your project and the `UseDownloadButton` option is `true`, the Download Button plugin will automatically add a download button to each code block in your Markdown content. Users can click this button to download the content of the code block as a text file.
27 changes: 20 additions & 7 deletions src/Markdig.Prism/Markdig.Prism.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Ilya Verbitskiy</Authors>
<Authors>Ilya Verbitskiy, Emil Schütt</Authors>
<Company>WebStoating s.r.o.</Company>
<Description>Syntax highlighting for Markdig using Prism.js library</Description>
<Copyright>Copyright © 2020 Ilya Verbitskiy</Copyright>
<PackageProjectUrl>https://github.com/ilich/Markdig.Prism</PackageProjectUrl>
<RepositoryUrl>https://github.com/ilich/Markdig.Prism</RepositoryUrl>
<Description>A fork of WebStoating.Markdig.Prism that was made by Ilya Verbitskiy

Syntax highlighting for Markdig using Prism.js library and options for plugins, more to come, link to original package https://github.com/ilich/Markdig.Prism </Description>
<Copyright>Copyright © 2023 Emil Schütt</Copyright>
<PackageProjectUrl>https://github.com/Retrokiller543/Markdig.Prism</PackageProjectUrl>
<RepositoryUrl>https://github.com/Retrokiller543/Markdig.Prism</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>markdown markdig</PackageTags>
<PackageId>WebStoating.Markdig.Prism</PackageId>
<PackageTags>markdown markdig prism</PackageTags>
<PackageId>WebStoating.Markdig.PrismWithPlugins</PackageId>
<Title>PrismJS with plugins</Title>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>This version added the UseDownloadBotton and UseLineNumbers to the pipeline.</PackageReleaseNotes>
<IncludeSymbols>False</IncludeSymbols>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Markdig" Version="0.22.0" />
</ItemGroup>
Expand Down
Loading
0