Autoscoping (#103)
* Add autoscoping with generated demo * Remove manual demo from all snippets * Add JavaScript inside IIFE * Align mouse-cursor-gradient-tracking.md to demo code * Match snippets to demo * Update CONTRIBUTING.md * Create reusable function for code extraction
This commit is contained in:
@ -3,7 +3,8 @@ const path = require('path')
|
||||
const marked = require('marked')
|
||||
const pretty = require('pretty')
|
||||
const caniuseDb = require('caniuse-db/data.json')
|
||||
const { toKebabCase, createElement, template, dom } = require('../utils/utils.js')
|
||||
const sass = require('node-sass')
|
||||
const { toKebabCase, createElement, template, dom, getCode } = require('../utils/utils.js')
|
||||
|
||||
const SNIPPETS_PATH = './snippets'
|
||||
const TAGS = [
|
||||
@ -69,7 +70,23 @@ TAGS.slice(1).forEach(tag => {
|
||||
for (const snippetFile of fs.readdirSync(SNIPPETS_PATH)) {
|
||||
const snippetPath = path.join(SNIPPETS_PATH, snippetFile)
|
||||
const snippetData = fs.readFileSync(snippetPath, 'utf8')
|
||||
const markdown = marked(snippetData, { renderer })
|
||||
|
||||
const html = getCode('html', snippetData).trim()
|
||||
const css = getCode('css', snippetData)
|
||||
const scopedCSS = sass.renderSync({
|
||||
data: `[data-scope="${snippetFile}"] { ${css} }`
|
||||
})
|
||||
const js = getCode('js', snippetData)
|
||||
|
||||
const demo =
|
||||
`<div class="snippet-demo" data-scope="${snippetFile}">${html}</div>` +
|
||||
`<style>${scopedCSS.css.toString()}</style>` +
|
||||
`${js ? `<script>(function(){${js}})();</script>` : ''}`
|
||||
|
||||
const markdown = marked(snippetData, { renderer }).replace(
|
||||
'<h4>Demo</h4>',
|
||||
`<h4>Demo</h4>${demo}`
|
||||
)
|
||||
const snippetEl = createElement(`<div class="snippet">${markdown}</div>`)
|
||||
snippetContainer.append(snippetEl)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user