threepipe
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

simple-code-preview.mjs 1.7KB

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