Skip to content

Commit

Permalink
通常の公開用URLも使えるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-dyoshikawa committed Sep 13, 2024
1 parent da86e1b commit 454eb98
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 0 additions & 2 deletions packages/zenn-cli/articles/305-example-embed-others.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ published: true

@[docswell](https://www.docswell.com/slide/LK7J5V/embed)

### embed用でないdocswell URL

@[docswell](https://www.docswell.com/s/ku-suke/LK7J5V-hello-docswell)

## jsfiddle
Expand Down
10 changes: 8 additions & 2 deletions packages/zenn-markdown-html/src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isValidHttpUrl,
isDocswellUrl,
extractYoutubeVideoParameters,
extractDocswellEmbedUrl,
} from './utils/url-matcher';

/* 埋め込み要素の種別 */
Expand Down Expand Up @@ -80,10 +81,15 @@ export const embedGenerators: Readonly<EmbedGeneratorList> = {
)}" scrolling="no" allowfullscreen allow="encrypted-media" loading="lazy"></iframe></span>`;
},
docswell(str) {
const errorMessage = 'DocswellのスライドURLが不正です';
if (!isDocswellUrl(str)) {
return 'Docswellのembed用のURLを指定してください';
return errorMessage;
}
return `<span class="embed-block embed-docswell"><iframe src="${str}" allowfullscreen="true" class="docswell-iframe" width="620" height="349" style="border: 1px solid #ccc; display: block; margin: 0px auto; padding: 0px; aspect-ratio: 620/349;"></iframe></span>`;
const slideUrl = extractDocswellEmbedUrl(str);
if (!slideUrl) {
return errorMessage;
}
return `<span class="embed-block embed-docswell"><iframe src="${slideUrl}" allowfullscreen="true" class="docswell-iframe" width="620" height="349" style="border: 1px solid #ccc; display: block; margin: 0px auto; padding: 0px; aspect-ratio: 620/349;"></iframe></span>`;
},
jsfiddle(str) {
if (!isJsfiddleUrl(str)) {
Expand Down
22 changes: 21 additions & 1 deletion packages/zenn-markdown-html/src/utils/url-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ export function isJsfiddleUrl(url: string): boolean {
return /^(http|https):\/\/jsfiddle\.net\/[a-zA-Z0-9_,/-]+$/.test(url);
}

const docswellNormalUrlRegex =
/^https:\/\/www\.docswell\.com\/s\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+$/;
const docswellEmbedUrlRegex =
/^https:\/\/www\.docswell\.com\/slide\/[a-zA-Z0-9_-]+\/embed$/;

export function isDocswellUrl(url: string): boolean {
return /^https:\/\/www\.docswell\.com\/[a-zA-Z0-9_,/-]+\/embed$/.test(url);
return [docswellNormalUrlRegex, docswellEmbedUrlRegex].some((pattern) =>
pattern.test(url)
);
}

export function isYoutubeUrl(url: string): boolean {
Expand Down Expand Up @@ -84,6 +91,19 @@ export function extractYoutubeVideoParameters(
return { videoId, start };
}

export function extractDocswellEmbedUrl(url: string): string | null {
// Embed用URLの場合、そのまま返す
if (docswellEmbedUrlRegex.test(url)) {
return url;
}
// Embed用URLでない場合 https://www.docswell.com/s/:username/{slideId}-hello-docswell のslideIdを抽出する
const slideId = new URL(url).pathname.split('/').at(3)?.split('-').at(0);
if (!slideId) {
return null;
}
return `https://www.docswell.com/slide/${slideId}/embed`;
}

/**
* 参考: https://blueprintue.com/
* 生成されるURLをもとに正規表現を定義した
Expand Down

0 comments on commit 454eb98

Please sign in to comment.