Allow embedding of GitLab snippets (#6217)

This commit is contained in:
Andrew Smith
2023-11-28 00:35:37 +11:00
committed by GitHub
parent ca737ab641
commit 8f53f3b28c
5 changed files with 88 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
import * as React from "react";
import styled from "styled-components";
import { EmbedProps as Props } from ".";
const GITLAB_NAMESPACE_REGEX = "(([a-zA-Z\\d-]+)/)";
const Iframe = styled.iframe`
margin-top: 8px;
`;
function GitLabSnippet(props: Props) {
const snippetUrl = new URL(props.attrs.href);
const id = snippetUrl.pathname.split("/").pop();
const snippetLink = `${snippetUrl}.js`;
const snippetScript = `<script type="text/javascript" src="${snippetLink}"></script>`;
const styles = "<style>body { margin: 0; }</style>";
const iframeHtml = `<html><head><base target="_parent">${styles}</head><body>${snippetScript}</body></html>`;
return (
<Iframe
src={`data:text/html;base64,${btoa(iframeHtml)}`}
className={props.isSelected ? "ProseMirror-selectednode" : ""}
frameBorder="0"
width="100%"
height="400px"
id={`gitlab-snippet-${id}`}
title="GitLab Snippet"
/>
);
}
GitLabSnippet.ENABLED = [
new RegExp(
`^https://gitlab\\.com/${GITLAB_NAMESPACE_REGEX}*-/snippets/\\d+$`
),
];
export default GitLabSnippet;