threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

simple-code-preview.mjs 1.4KB

3 лет назад
3 лет назад
3 лет назад
12345678910111213141516171819202122232425
  1. import {setupCodePreview} from 'https://cdn.jsdelivr.net/gh/repalash/example-code-previewer/dist/index.js';
  2. const exampleScript = document.getElementById('example-script')
  3. const scripts = exampleScript && exampleScript.dataset.scripts ? exampleScript.dataset.scripts.split(';') : []
  4. if(exampleScript.textContent) scripts.push(exampleScript)
  5. const exampleStyle = document.querySelector('#example-style')
  6. const css = exampleStyle ? exampleStyle.textContent : ''
  7. const importMap = document.querySelector('script[type="importmap"]')
  8. const imports = JSON.parse(importMap.textContent||'{}').imports||{}
  9. imports['threepipe'] = 'https://threepipe.org/dist/index.mjs'
  10. function replaceImports(code) {
  11. for (const [name, link] of Object.entries(imports)) code = code.replaceAll(` from '${name}'`, ` from '${link}'`)
  12. return code
  13. }
  14. setupCodePreview(
  15. document.getElementById('canvas-container') || document.querySelector('.code-preview-container'),
  16. scripts,
  17. scripts.map(s=>s.textContent ? 'js' : s.split('.').pop()), // title
  18. scripts.map(s=>(typeof s === 'string' && s.endsWith('.js')) ? s : 'https://github.com/repalash/threepipe/tree/master/examples/'+ window.location.pathname.split('/examples/').pop().replace('index.html', '')+(s.textContent ? 'index.html' : s)), // todo: github link
  19. (c) => replaceImports(c).replaceAll(` from '../`, ` from 'https://threepipe.org/examples/`),
  20. {
  21. title: 'ThreePipe: ' + document.title,
  22. css,
  23. },
  24. );