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

HTML/SVG code preview #3259

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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/inscriptions/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) enum Media {
Markdown,
Model,
Pdf,
Source,
Text,
Unknown,
Video,
Expand Down Expand Up @@ -97,8 +98,8 @@ impl Media {
("model/gltf-binary", GENERIC, Model, &["glb"]),
("model/stl", GENERIC, Unknown, &["stl"]),
("text/css", TEXT, Code(Css), &["css"]),
("text/html", TEXT, Iframe, &[]),
("text/html;charset=utf-8", TEXT, Iframe, &["html"]),
("text/html", TEXT, Source, &[]),
("text/html;charset=utf-8", TEXT, Source, &["html"]),
("text/javascript", TEXT, Code(JavaScript), &["js"]),
("text/markdown", TEXT, Markdown, &[]),
("text/markdown;charset=utf-8", TEXT, Markdown, &["md"]),
Expand Down
14 changes: 13 additions & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
templates::{
BlockHtml, BlocksHtml, ChildrenHtml, ClockSvg, CollectionsHtml, HomeHtml, InputHtml,
InscriptionHtml, InscriptionsBlockHtml, InscriptionsHtml, OutputHtml, PageContent, PageHtml,
PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml,
PreviewAudioHtml, PreviewCodeHtml, PreviewSourceHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml,
PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml,
RangeHtml, RareTxt, RuneBalancesHtml, RuneHtml, RunesHtml, SatHtml, TransactionHtml,
},
Expand Down Expand Up @@ -1378,6 +1378,18 @@ impl Server {
)
.into_response(),
),
Media::Source => Ok(
(
[(
header::CONTENT_SECURITY_POLICY,
"script-src-elem 'self' https://cdn.jsdelivr.net",
)],
PreviewSourceHtml {
inscription_id,
},
)
.into_response(),
),
Media::Font => Ok(
(
[(
Expand Down
2 changes: 1 addition & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) use {
metadata::MetadataHtml,
output::OutputHtml,
preview::{
PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml,
PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewSourceHtml, PreviewImageHtml, PreviewMarkdownHtml,
PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml,
},
range::RangeHtml,
Expand Down
7 changes: 6 additions & 1 deletion src/templates/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ pub(crate) struct PreviewCodeHtml {
pub(crate) language: media::Language,
}

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

#[derive(boilerplate::Boilerplate)]
pub(crate) struct PreviewFontHtml {
pub(crate) inscription_id: InscriptionId,
}
Expand Down
22 changes: 22 additions & 0 deletions static/preview-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

const definition = await import(`https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11/build/es/languages/xml.min.js`);

hljs.registerLanguage('xml', definition.default);

const inscription = document.documentElement.dataset.inscription;

const escapeHtml = (htmlCode) => {
return htmlCode
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}


const response = await fetch(`/content/${inscription}`);
const text = await response.text();
const code = document.querySelector('code');

code.innerHTML = escapeHtml(text)
11 changes: 11 additions & 0 deletions templates/preview-source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang=en data-inscription={{self.inscription_id}} >
<head>
<meta charset=utf-8>
<link rel=stylesheet href=/static/preview-source.css>
<script src=/static/preview-source.js defer type=module></script>
</head>
<body>
<pre><code></code></pre>
</body>
</html>
Loading