-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #577 from osfameron/markdown-macro-prod
Asciidoc markdown block (merge to prod)
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
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,36 @@ | ||
= Site Extensions | ||
|
||
Test and document your Asciidoctor.js or Antora extensions here. | ||
|
||
== Markdown Block | ||
|
||
[source,asciidoc] | ||
---- | ||
[markdown] | ||
-- | ||
Filter some text with [Markdown](https://commonmark.org/help/) syntax. | ||
-- | ||
---- | ||
|
||
Results in: | ||
|
||
[markdown] | ||
-- | ||
Filter some text with [Markdown](https://commonmark.org/help/) syntax. | ||
-- | ||
|
||
|
||
[NOTE] | ||
-- | ||
This is not implemented with a full Markdown parser. | ||
See link:https://github.com/asciidoctor/kramdown-asciidoc/issues/7[issue] | ||
with a link to the "naive series of regexes" used as starting point. | ||
|
||
(And note that we use Open | ||
link:https://docs.asciidoctor.org/asciidoc/latest/blocks/delimited/#summary-of-structural-containers[structural context], | ||
with `--` delimiters, and headings don't work inside these.) | ||
|
||
This feature is intended for handling OpenAPI specs, which can contain Markdown, | ||
however openapi-generator has link:https://github.com/OpenAPITools/openapi-generator/issues/11396[poor Asciidoc handling], | ||
so instead we add the block delimiters in the template, and let the block filter handle it. | ||
-- |
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,42 @@ | ||
// from https://github.com/dohliam/markdoctor/blob/master/js/mdtoadoc.js | ||
function convMdAdoc(txt) { | ||
// horizontal rules | ||
txt = txt.replace(/^\-{3,}$/gm, "'''").replace(/^[\*_]{3,}/gm, "'''"); | ||
|
||
// general text formatting | ||
txt = txt.replace(/^\*([^\s\*][^\*]+)\*/gm, "_$1_").replace(/\s\*([^\*\s]+)\*/g, " _$1_").replace(/\*\*_/g, "*_").replace(/_\*\*/g, "_*").replace(/^\*\*([^\*]+)\*\*/gm, "*$1*").replace(/\s\*\*([^\*]+)\*\*/g, " *$1*"); | ||
|
||
// headings | ||
txt = txt.replace(/^#*/gm, (x) => '='.repeat(x.length)) | ||
|
||
// images | ||
txt = txt.replace(/\[\!\[([^\]]*)\]\(([^\)]+)\)\]\(([^\)]+)\)/g, "image::$2[$1, link=\"$3\"]").replace(/\!\[([^\]]*)\]\(([^\)]+)\)/g, "image::$2[$1]"); | ||
|
||
// links | ||
txt = txt.replace(/\[([^\]]+)\]\(([a-z\.\/]+[^:\)]+)\)/g, "link:$2[$1]").replace(/\[([^\]]+)\]\(([a-z]+:\/\/[^\)]+)\)/g, "$2[$1]"); | ||
|
||
// unordered lists | ||
txt = txt.replace(/^\s{2}[\*\-]\s/gm, "** ").replace(/^\s{4}[\*\-]\s/gm, "*** ").replace(/^\s{6}[\*\-]\s/gm, "**** ").replace(/^\s{8}[\*\-]\s/gm, "***** "); | ||
|
||
// ordered lists | ||
txt = txt.replace(/^\d+\.\s/gm, ". ").replace(/^\s{2}\d+\.\s/gm, ".. ").replace(/^\s{4}\d+\.\s/gm, "... ").replace(/^\s{6}\d+\.\s/gm, ".... ").replace(/^\s{8}\d+\.\s/gm, "..... "); | ||
|
||
// tables | ||
txt = txt.replace(/^\|*\s*(.*?)\s+\|\s+(.*?)\n\|*\s*\-+.*\n/gm, "[options=\"header\"]\n|===\n| $1 | $2\n").replace(/^\|*\s*(.*?\s+\|\s+.*?)\n\n/gm, "| $1\n|===\n\n").replace(/^([^\|]+\s+\|\s+.*$)/gm, "| $1"); | ||
|
||
return txt | ||
} | ||
|
||
module.exports = function (registry) { | ||
registry.block(function () { | ||
var self = this | ||
self.named('markdown') | ||
self.onContext('open') | ||
self.process(function (parent, reader) { | ||
|
||
var lines = reader.getLines().map(function (l) { return convMdAdoc(l) }) | ||
const source = lines.join('\n') | ||
return self.createOpenBlock(parent, source) | ||
}) | ||
}) | ||
} |