Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preview-markdown #2325

Merged
merged 19 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) enum Media {
Audio,
Iframe,
Image,
Markdown,
Model,
Pdf,
Text,
Expand Down Expand Up @@ -41,8 +42,8 @@ impl Media {
("text/html", Media::Iframe, &[]),
("text/html;charset=utf-8", Media::Iframe, &["html"]),
("text/javascript", Media::Text, &["js"]),
("text/markdown", Media::Text, &[]),
("text/markdown;charset=utf-8", Media::Text, &["md"]),
("text/markdown", Media::Markdown, &[]),
("text/markdown;charset=utf-8", Media::Markdown, &["md"]),
("text/plain", Media::Text, &[]),
("text/plain;charset=utf-8", Media::Text, &["txt"]),
("video/mp4", Media::Video, &["mp4"]),
Expand Down
36 changes: 33 additions & 3 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use {
crate::templates::{
BlockHtml, ClockSvg, HomeHtml, InputHtml, InscriptionHtml, InscriptionJson,
InscriptionsBlockHtml, InscriptionsHtml, InscriptionsJson, OutputHtml, OutputJson, PageContent,
PageHtml, PreviewAudioHtml, PreviewImageHtml, PreviewModelHtml, PreviewPdfHtml,
PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml, RangeHtml, RareTxt, SatHtml, SatJson,
TransactionHtml,
PageHtml, PreviewAudioHtml, PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml,
PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml, RangeHtml, RareTxt,
SatHtml, SatJson, TransactionHtml,
},
axum::{
body,
Expand Down Expand Up @@ -915,6 +915,16 @@ impl Server {
)
.into_response(),
),
Media::Markdown => Ok(
(
[(
header::CONTENT_SECURITY_POLICY,
"script-src-elem 'self' https://cdn.jsdelivr.net",
)],
PreviewMarkdownHtml { inscription_id },
)
.into_response(),
),
Media::Model => Ok(
(
[(
Expand Down Expand Up @@ -2514,6 +2524,26 @@ mod tests {
);
}

#[test]
fn markdown_preview() {
let server = TestServer::new_with_regtest();
server.mine_blocks(1);

let txid = server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, inscription("text/markdown", "hello").to_witness())],
..Default::default()
});
let inscription_id = InscriptionId { txid, index: 0 };

server.mine_blocks(1);

server.assert_response_regex(
format!("/preview/{inscription_id}"),
StatusCode::OK,
format!(r".*<html lang=en data-inscription={inscription_id}>.*"),
);
}

#[test]
fn image_preview() {
let server = TestServer::new_with_regtest();
Expand Down
4 changes: 2 additions & 2 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub(crate) use {
output::{OutputHtml, OutputJson},
page_config::PageConfig,
preview::{
PreviewAudioHtml, PreviewImageHtml, PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml,
PreviewUnknownHtml, PreviewVideoHtml,
PreviewAudioHtml, PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml, PreviewPdfHtml,
PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml,
},
range::RangeHtml,
rare::RareTxt,
Expand Down
5 changes: 5 additions & 0 deletions src/templates/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub(crate) struct PreviewImageHtml {
pub(crate) inscription_id: InscriptionId,
}

#[derive(boilerplate::Boilerplate)]
pub(crate) struct PreviewMarkdownHtml {
pub(crate) inscription_id: InscriptionId,
}

#[derive(boilerplate::Boilerplate)]
pub(crate) struct PreviewModelHtml {
pub(crate) inscription_id: InscriptionId,
Expand Down
7 changes: 7 additions & 0 deletions static/preview-markdown.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
html {
color: #98a3ad;
background-color: #131516;
max-width: 900px;
margin: 0 auto;
font-family: system-ui, sans-serif;
}
6 changes: 6 additions & 0 deletions static/preview-markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { marked } from 'https://cdn.jsdelivr.net/npm/marked@8.0.0/+esm'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use version 9


const inscription = document.documentElement.dataset.inscription;
const response = await fetch(`/content/${inscription}`);
const markdown = await response.text();
document.body.innerHTML = marked.parse(markdown);
10 changes: 10 additions & 0 deletions templates/preview-markdown.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<html lang=en data-inscription={{self.inscription_id}}>
<head>
<meta charset=utf-8>
<link rel=stylesheet href=/static/preview-markdown.css></link>
<script src=/static/preview-markdown.js type=module defer></script>
</head>
<body>
</body>
</html>
Loading