marketplace.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>Logseq Marketplace</title>
  9. <link rel="stylesheet"
  10. href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/4.0.0/github-markdown.min.css"
  11. integrity="sha512-Oy18vBnbSJkXTndr2n6lDMO5NN31UljR8e/ICzVPrGpSud4Gkckb8yUpqhKuUNoE+o9gAb4O/rAxxw1ojyUVzg=="
  12. crossorigin="anonymous" referrerpolicy="no-referrer"/>
  13. <style>
  14. html, body {
  15. padding: 0;
  16. margin: 0;
  17. box-sizing: border-box;
  18. font-family: sans-serif;
  19. width: 100%;
  20. height: 100%;
  21. }
  22. body {
  23. display: flex;
  24. }
  25. #app {
  26. display: flex;
  27. justify-content: center;
  28. flex: 1;
  29. width: 900px;
  30. min-height: 100%;
  31. padding: 30px;
  32. margin: 0 auto;
  33. color: #333;
  34. }
  35. #app > strong {
  36. padding-top: 30vh;
  37. font-weight: 400;
  38. }
  39. pre {
  40. max-width: 800px;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div id="app"></div>
  46. <script src="./js/marked.min.js"></script>
  47. <script>
  48. ;(async function () {
  49. const app = document.getElementById('app')
  50. const url = new URL(location.href)
  51. const setMsg = (msg) => app.innerHTML = `<strong>${msg}</strong>`
  52. const repo = url.searchParams.get('repo')
  53. if (!repo) {
  54. return setMsg('Repo parameter not found!')
  55. }
  56. const setContent = (content) => app.innerHTML = `<main class="markdown-body">${content}</main>`
  57. const endpoint = (repo, branch, file) => `https://raw.githubusercontent.com/${repo}/${branch}/${file}`
  58. let content = ''
  59. let points = [
  60. endpoint(repo, 'master', 'README.md'), endpoint(repo, 'main', 'README.md'),
  61. endpoint(repo, 'master', 'readme.md'), endpoint(repo, 'main', 'readme.md')]
  62. let readme
  63. setMsg('Loading ...')
  64. for (let url of points) {
  65. try {
  66. const res = await fetch(url)
  67. if (res.status !== 200) {
  68. throw new Error(res.statusText)
  69. }
  70. content = await res.text()
  71. readme = url
  72. break
  73. } catch (e) {
  74. console.debug('Error:', url, e.message)
  75. }
  76. }
  77. const fixLink = (link) => readme.replace(/README\.md$/i, link)
  78. const isRelative = (href) => href && !href.startsWith('http') && !!href.match(/^([.\/]+|[^\/]+)/) && href.replace(/^[.\/]+/, '')
  79. marked.use({
  80. renderer: {
  81. link (href, title, text) {
  82. return `<a href="${href}" target="_blank" title="${title}">${text}</a>`
  83. },
  84. image (href, title, text) {
  85. let link = isRelative(href)
  86. if (link) {
  87. link = fixLink(link)
  88. return `<img style="max-width: 100%;" src="${link}" alt="${title}" />`
  89. }
  90. return false
  91. }
  92. }
  93. })
  94. content = marked(content).replace('src="./', `src="${fixLink('')}`)
  95. setContent(content)
  96. }())
  97. </script>
  98. </body>
  99. </html>