Skip to content

Commit

Permalink
Support include files in markdown, refs Mermade#20
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRalphson committed Dec 14, 2017
1 parent 699a45a commit 92ab01b
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ compare.js
doit.sh
source/layouts/layout*.erb
spectacle
dev/

# Logs
logs
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ options.minify = false;
options.customCss = false;
options.inline = false;
options.unsafe = false; // setting to true turns off markdown sanitisation
//options.source = filename; // used to resolve relative paths for included files
shins.render(markdownString, options, function(err, html) {
// ...
});
Expand All @@ -58,6 +59,7 @@ options.minify = false;
options.customCss = false;
options.inline = false;
options.unsafe = false; // setting to true turns off markdown sanitisation
//options.source = filename; // used to resolve relative paths for included files
shins.render(markdownString, options)
.then(html => {
// ...
Expand Down Expand Up @@ -86,6 +88,8 @@ Setting `inline` to `true` will inline all page resources (except resources refe
* For converting [OpenApi / Swagger](https://github.com/OAI/OpenAPI-Specification) or [AsyncAPI](https://github.com/asyncapi/asyncapi) definitions to Shins or Slate, see [widdershins](http://github.com/mermade/widdershins)
* If you need a CLI with more control over the options, why not try [make-shins](https://github.com/cazzer/make-shins)
* Shins ships with an alternate theme by TradeGecko which is also under the Apache 2.0 license
* Shins supports [AsciiDoc](http://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#include-files) `include::filename[]` syntax as well as `!INCLUDE filename` from [markdown-pp](https://github.com/MikeRalphson/markdown-pp-js)
* If you are using Node.js 4, please specify the `--harmony` flag

### Shins in the wild

Expand Down
4 changes: 3 additions & 1 deletion arapaho.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function check(req,res,fpath) {
catch (ex) { }
if (srcStat.mtime>dstStat.mtime) {
console.log('Rebuilding '+fpath);
fs.readFile(path.join(__dirname,'source',fpath+'.md'),'utf8',function(err,markdown){
let source = path.join(__dirname,'source',fpath+'.md');
fs.readFile(source,'utf8',function(err,markdown){
if (markdown) {
let options = {};
if (req.query["customcss"]) {
Expand All @@ -35,6 +36,7 @@ function check(req,res,fpath) {
if (req.query["minify"]) {
options.minify = true;
}
options.source = source;
shins.render(markdown,options,function(err,html){
if (err) {
console.warn(err);
Expand Down
27 changes: 26 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ function language_array(language_tabs) {
return JSON.stringify(result).split('"').join('"');
}

function preProcess(content,options) {
let lines = content.split('\r').join('').split('\n');
for (let l=0;l<lines.length;l++) {
let line = lines[l];
let filename = '';
if (line.startsWith('include::') && line.endsWith('[]')) { // asciidoc syntax
filename = line.split(':')[2].replace('[]','');
}
else if (line.startsWith('!INCLUDE ')) { // markdown-pp syntax
filename = line.replace('!INCLUDE ','');
}
if (filename) {
if (options.source) filename = path.resolve(path.dirname(options.source),filename);
let s = fs.readFileSync(filename,'utf8');
let include = s.split('\r').join('').split('\n');
lines.splice(l,1,...include);
}
else lines[l] = line;
}
return lines.join('\n');
}

function postProcess(content) {
// adds id a la GitHub autolinks to automatically-generated headers
content = content.replace(/\<(h[123456])\>(.*)\<\/h[123456]\>/g, function (match, group1, group2) {
Expand Down Expand Up @@ -189,7 +211,10 @@ function render(inputStr, options, callback) {
hljs.registerLanguage('shell', function (hljs) { return sh; });
hljs.registerLanguage('sh', function (hljs) { return sh; });

var content = md.render(clean(inputArr[2]));
while (inputArr.length<3) inputArr.push('');

var content = preProcess(inputArr[2],options);
content = md.render(clean(content));
content = postProcess(content);

var locals = {};
Expand Down
167 changes: 167 additions & 0 deletions master.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@



<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Master/Include Sample v1.0.0</title>

<style>
</style>
<link rel="stylesheet" media="screen" href="pub/css/screen.css">
<link rel="stylesheet" media="print" href="pub/css/print.css">
<link rel="stylesheet" media="screen" href="pub/css/darkula.css">

<script src="source/javascripts/lib/_jquery.js"></script>
<script src="source/javascripts/lib/_jquery.highlight.js"></script>
<script src="source/javascripts/lib/_lunr.js"></script>
<script src="source/javascripts/lib/_imagesloaded.min.js"></script>
<script src="source/javascripts/lib/_energize.js"></script>
<script src="source/javascripts/app/_lang.js"></script>
<script src="source/javascripts/app/_search.js"></script>
<script src="source/javascripts/app/_toc.js"></script>
<script type="text/javascript">
$(function() {
loadToc($('#toc'), '.toc-link', '.toc-list-h2, .toc-list-h3, .toc-list-h4, .toc-list-h5, .toc-list-h6', 10);
setupLanguages($('body').data('languages'));
$('.content').imagesLoaded( function() {
recacheHeights();
refreshToc();
});
});

window.onpopstate = function() {
activateLanguage(getLanguageFromQueryString());
};
</script>


<meta name="theme-color" content="#F3F7F9" />

</head>

<body data-languages="[&quot;javascript--nodejs&quot;,&quot;javascript&quot;,&quot;python&quot;,&quot;ruby&quot;,&quot;java&quot;,&quot;go&quot;]">
<a href="#" id="nav-button">
<span>
NAV
<img src="source/images/navbar.png" class="undefined" alt="Navigation">
</span>
</a>
<div class="toc-wrapper">
<img src="source/images/logo.png" class="logo" alt="Logo">

<div class="lang-selector">


<a href="#" data-language-name="javascript--nodejs">Node.JS</a>



<a href="#" data-language-name="javascript">JavaScript</a>



<a href="#" data-language-name="python">Python</a>



<a href="#" data-language-name="ruby">Ruby</a>



<a href="#" data-language-name="java">Java</a>



<a href="#" data-language-name="go">Go</a>


</div>


<div class="search">
<input type="text" class="search" id="input-search" placeholder="Search">
</div>
<ul class="search-results"></ul>

<div id="toc" class="toc-list-h1">
<ul class="toc-list-h1">

<li>
<a href="#master-content" class="toc-h1 toc-link" data-title="Master content">Master content</a>

<ul class="toc-list-h2">

<li>
<a href="#included-content" class="toc-h2 toc-link" data-title="Included content">Included content</a>

</li>

<li>
<a href="#included-content-2" class="toc-h2 toc-link" data-title="Included content 2">Included content 2</a>

</li>

</ul>

</li>

</ul>
</div>

<ul class="toc-footer">

<li><a href="https://mermade.github.io/shins/index.html">See OpenAPI example</a></li>

</ul>

</div>
<div class="page-wrapper">
<div class="dark-box"></div>
<div class="content">
<h1 id="master-content">Master content</h1>
<h2 id="included-content">Included content</h2>
<h3 id=""></h3>
<p>This content comes from <code>include.md</code></p>
<h2 id="included-content-2">Included content 2</h2>
<h3 id=""></h3>
<p>This content comes from <code>include.md</code></p>

</div>
<div class="dark-box">

<div class="lang-selector">


<a href="#" data-language-name="javascript--nodejs">Node.JS</a>



<a href="#" data-language-name="javascript">JavaScript</a>



<a href="#" data-language-name="python">Python</a>



<a href="#" data-language-name="ruby">Ruby</a>



<a href="#" data-language-name="java">Java</a>



<a href="#" data-language-name="go">Go</a>


</div>

</div>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions shins.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if (process.argv.length > 2) {
}

var inputStr = fs.readFileSync(inputName,'utf8');
options.source = inputName;

shins.render(inputStr,options,function(err,str){
if (err) {
Expand Down
3 changes: 3 additions & 0 deletions source/include.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
###

This content comes from `include.md`
26 changes: 26 additions & 0 deletions source/master.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Master/Include Sample v1.0.0
language_tabs:
- javascript--nodejs: Node.JS
- javascript: JavaScript
- python: Python
- ruby: Ruby
- java: Java
- go: Go
toc_footers:
- '<a href="https://mermade.github.io/shins/index.html">See OpenAPI example</a>'
includes: []
search: true
highlight_theme: darkula
---

# Master content

## Included content

include::include.md[]

## Included content 2

!INCLUDE include.md

0 comments on commit 92ab01b

Please sign in to comment.