Skip to content
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

Add 2 new Markdown IT Functionality (include and container) #132

Merged
merged 15 commits into from
Dec 8, 2021
Merged
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ A markdown-converter for [vscode][vscode]
- [Custom Date-Formats](#custom-date-formats)
- [Headers and Footers](#headers-and-footers)
- [Including Table of Contents](#including-table-of-contents)
- [Including Markdown Fragment Files](#including-markdown-fragment-files)
- [Creating Block-Level Custom Containers](#creating-block-level-custom-containers)
- [Third Party Markdown Extensions](#third-party-markdown-extensions)
- [Assets](#assets)
- [CSS- and JavaScript-Files](#css--and-javascript-files)
Expand Down Expand Up @@ -191,6 +193,32 @@ Furthermore, the `markdownConverter.Parser.Toc.Levels` allows you to choose whic
}
```

## Including Markdown Fragment Files
It is now possible to fragment the document in multiple sections and to merge them all into one. This way, it's possible to work on separate fragments at the same time without having to bear with conflicts during editing. Add fragments using this syntax:
```md
!!!include(file.md)!!!
```

## Creating Block-Level Custom Containers
MarkdownConverter allows you to apply CSS classes to specific parts of your document.
This feature can be used with the following syntax:

In your Markdown file:
```md
:::class

Text Here

:::
```

In your CSS file:
```css
div.class {
// Custom CSS Here
}
```

## Third Party Markdown Extensions
If you want to use third party markdown-extensions in your documents (such as the [Markdown Preview Mermaid Support], [Markdown Footnote] or [Markdown Emoji]), you might want to install the extensions of your choice and enable the `markdownConverter.Parser.SystemParserEnabled` setting which makes `MarkdownConverter` use VSCode's built-in `markdown-it` parser with all markdown extensions enabled.

Expand Down
52 changes: 51 additions & 1 deletion package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"markdown-it": "^12.2.0",
"markdown-it-anchor": "^8.3.1",
"markdown-it-checkbox": "^1.1.0",
"markdown-it-container": "^3.0.0",
"markdown-it-emoji": "^2.0.0",
"markdown-it-table-of-contents": "^0.5.2",
"multi-integer-range": "^4.0.9",
Expand All @@ -95,6 +96,7 @@
"@types/highlight.js": "^9.12.4",
"@types/lodash.clonedeep": "^4.5.6",
"@types/lodash.kebabcase": "^4.1.6",
"@types/markdown-it-container": "^2.0.4",
"@types/markdown-it-emoji": "^2.0.2",
"@types/minimist": "^1.2.2",
"@types/mocha": "^9.0.0",
Expand All @@ -112,6 +114,7 @@
"eslint": "^7.32.0",
"json5-loader": "^4.0.1",
"lodash.kebabcase": "^4.1.1",
"markdown-it-include": "^2.0.0",
"minimist": "^1.2.5",
"mocha": "^9.1.2",
"ovsx": "^0.2.1",
Expand Down
10 changes: 10 additions & 0 deletions src/System/Documents/EnvironmentKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Represents a key
*/
export enum EnvironmentKey
{
/**
* Indicates the {@link RootDir `RootDir`} setting.
*/
RootDir = "markdown-converter-root-dir"
}
8 changes: 7 additions & 1 deletion src/System/Documents/MarkdownFragment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { dirname } from "upath";
import { Document } from "./Document";
import { DocumentFragment } from "./DocumentFragment";
import { EnvironmentKey } from "./EnvironmentKey";

/**
* Represents a fragment of a document with markdown-support.
Expand All @@ -25,6 +27,10 @@ export class MarkdownFragment extends DocumentFragment
*/
protected override async RenderContent(): Promise<string>
{
return this.Document.Parser.render(await super.RenderContent());
return this.Document.Parser.render(
await super.RenderContent(),
{
[EnvironmentKey.RootDir]: this.Document.FileName ? dirname(this.Document.FileName) : null
});
}
}
42 changes: 42 additions & 0 deletions src/System/Tasks/ConversionRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { highlight } from "highlight.js";
import cloneDeep = require("lodash.clonedeep");
import MarkdownIt = require("markdown-it");
import checkbox = require("markdown-it-checkbox");
import markdownContainer = require("markdown-it-container");
import emoji = require("markdown-it-emoji");
import markdownInclude = require("markdown-it-include");
import StateCore = require("markdown-it/lib/rules_core/state_core");
import Token = require("markdown-it/lib/token");
import format = require("string-template");
import { convert, parse } from "twemoji";
import { dirname, isAbsolute, join, resolve } from "upath";
Expand All @@ -25,6 +29,7 @@ import { WebScript } from "../Documents/Assets/WebScript";
import { AttributeKey } from "../Documents/AttributeKey";
import { Document } from "../Documents/Document";
import { EmojiType } from "../Documents/EmojiType";
import { EnvironmentKey } from "../Documents/EnvironmentKey";
import { ListType } from "../Documents/ListType";
import { Anchor } from "../Documents/Plugins/MarkdownAnchorPlugin";
import { TOC } from "../Documents/Plugins/MarkdownTocPlugin";
Expand Down Expand Up @@ -489,6 +494,43 @@ export class ConversionRunner
};
}

/**
* Add Markdown IT Container
*/
parser.use(
markdownContainer,
"",
{
validate(name: string)
{
return name.trim().length;
},
render(token: Token[], id: number)
{
if (token[id].info.trim() !== "")
{
return `<div class="${token[id].info.trim()}">\n`;
}
else
{
return "</div>\n";
}
}
});

/**
* Add Markdown IT Include
*/
parser.use(
markdownInclude,
{
getRootDir: (options: any, state: StateCore) =>
{
return state.env[EnvironmentKey.RootDir] ?? ".";
},
includeRe: /!{3}\s*include(.+?)!{3}/i
});

L3houx marked this conversation as resolved.
Show resolved Hide resolved
return parser;
}

Expand Down
1 change: 1 addition & 0 deletions src/System/markdown-it-include.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "markdown-it-include";
3 changes: 2 additions & 1 deletion webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export = (env: any, argv: any) =>
path: resolve(__dirname, "lib"),
filename: "[name].js",
libraryTarget: "commonjs2",
devtoolFallbackModuleFilenameTemplate: "../[resource-path]"
devtoolFallbackModuleFilenameTemplate: "../[resource-path]",
hashFunction: "xxhash64"
L3houx marked this conversation as resolved.
Show resolved Hide resolved
},
devtool: "source-map",
externals,
Expand Down