-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gh-pages): the ability to include markdown into demo pages (#368)
Co-authored-by: Anna <[email protected]>
- Loading branch information
Showing
32 changed files
with
165 additions
and
36 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 @@ | ||
.markdown-container { | ||
& ul { | ||
padding-left: 1.5rem; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
@import (inline) "./libs/bootstrap.css"; | ||
@import "./common/markdown.less"; | ||
@import "./common/page.less"; | ||
@import "./header/header.less"; | ||
|
||
@import "../../src/modules/all.less"; | ||
@import "../../src/modules/draft/all.less"; | ||
|
||
@import (inline) "../../node_modules/highlight.js/styles/github.css"; |
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,16 @@ | ||
const hljs = require('highlight.js'); | ||
const markdownLib = require('markdown-it'); | ||
|
||
const markdown = markdownLib({ | ||
html: true, | ||
highlight: function (str, lang) { | ||
if (lang && hljs.getLanguage(lang)) { | ||
try { | ||
return hljs.highlight(str, { language: lang }).value; | ||
} catch { } | ||
} | ||
return ''; | ||
} | ||
}); | ||
|
||
module.exports = { markdown }; |
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,55 @@ | ||
const path = require('path'); | ||
const fsAsync = require('fs').promises; | ||
const { JSDOM } = require('jsdom'); | ||
|
||
const { markdown } = require('./markdown'); | ||
|
||
const fileCache = new Map(); | ||
|
||
const parseFile = async (filePath) => { | ||
const absolutePath = path.resolve(__dirname, '../../../', filePath); | ||
|
||
if (fileCache.has(absolutePath)) return fileCache.get(absolutePath); | ||
|
||
const data = await fsAsync.readFile(absolutePath); | ||
const content = data.toString(); | ||
const renderedContent = markdown.render(content); | ||
|
||
fileCache.set(absolutePath, renderedContent); | ||
return renderedContent; | ||
}; | ||
|
||
// <p><a name></a></p> | ||
const unwrapAnchor = (anchor) => anchor && anchor.matches(':only-child') ? anchor.parentElement : anchor; | ||
|
||
class MDRenderer { | ||
static wrapContent(content) { | ||
return `<div class="markdown-container">${content}</div>`; | ||
} | ||
|
||
static async render(filePath, startSel, endSel) { | ||
try { | ||
const content = await parseFile(filePath); | ||
|
||
if (!startSel) return MDRenderer.wrapContent(content); | ||
|
||
const { window } = new JSDOM(content); | ||
const startAnchor = window.document.querySelector(`a[name='${startSel}']`); | ||
const endAnchor = window.document.querySelector(`a[name='${endSel}']`); | ||
|
||
let node = unwrapAnchor(startAnchor); | ||
let endNode = unwrapAnchor(endAnchor); | ||
let partContent = ''; | ||
|
||
for (node = node.nextSibling; !!node && node !== endNode; node = node.nextSibling) { | ||
partContent += node.outerHTML || node.textContent; | ||
} | ||
|
||
return MDRenderer.wrapContent(partContent); | ||
} catch (e) { | ||
return `Rendering error: ${e}`; | ||
} | ||
} | ||
} | ||
|
||
module.exports = { MDRenderer }; |
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
Oops, something went wrong.