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

ドクセル埋め込み対応 #509

Merged
merged 8 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions packages/zenn-cli/articles/305-example-embed-others.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ published: true

@[speakerdeck](4f926da9cb4cd0001f00a1ff)

## docswell

@[docswell](https://www.docswell.com/slide/LK7J5V/embed)
cm-igarashi-ryosuke marked this conversation as resolved.
Show resolved Hide resolved

### embed用でないdocswell URL

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

## jsfiddle

@[jsfiddle](https://jsfiddle.net/9wkngdue/embedded)
Expand Down
1 change: 1 addition & 0 deletions packages/zenn-content-css/src/_embed.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ span.embed-block {
}
.embed-slideshare,
.embed-speakerdeck,
.embed-docswell,
Copy link
Contributor Author

@cm-dyoshikawa cm-dyoshikawa Sep 12, 2024

Choose a reason for hiding this comment

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

padding-bottomハックで対応する(やや縦が長いスライドの吸収対応になる)

参考: #321 (comment)

.embed-codepen,
.embed-jsfiddle,
.embed-youtube,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, test, expect } from 'vitest';
import markdownToHtml from '../../../src/index';

describe('Docswell', () => {
test('should generate docswell html', () => {
const html = markdownToHtml(
'@[docswell](https://www.docswell.com/slide/LK7J5V/embed)'
);
expect(html).toContain(
'<span class="embed-block embed-docswell"><iframe src="https://www.docswell.com/slide/LK7J5V/embed" allowfullscreen="true" width="620" style="border:1px solid #ccc;display:block;margin:0px auto;padding:0px;aspect-ratio:620/349"></iframe></span>'
);
});

test('should not generate docswell html with invalid url', () => {
const html = markdownToHtml(
'@[docswell](https://www.docswell.com/s/ku-suke/LK7J5V-hello-docswell)'
);
expect(html).toContain('Docswellのembed用のURLを指定してください');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { isDocswellUrl } from '../../src/utils/url-matcher';
import { describe, test, expect } from 'vitest';

describe('isDocswellUrlのテスト', () => {
describe('Docswellの埋め込み用URLのとき', () => {
test('trueを返すこと', () => {
const docswellEmbedUrl = 'https://www.docswell.com/slide/LK7J5V/embed';
expect(isDocswellUrl(docswellEmbedUrl)).toBe(true);
});
});

describe('Docswellの他の画面のURLのとき', () => {
test('falseを返すこと', () => {
const docswellUrls = [
'https://www.docswell.com/',
'https://www.docswell.com/s/ku-suke/LK7J5V-hello-docswell',
];

docswellUrls.forEach((url) => {
expect(isDocswellUrl(url)).toBe(false);
});
});
});

describe('他のサイトのURLのとき', () => {
test('falseを返すこと', () => {
const otherSiteUrls = ['https://zenn.dev/', 'https://github.com/'];

otherSiteUrls.forEach((url) => {
expect(isDocswellUrl(url)).toBe(false);
});
});
});
});
12 changes: 11 additions & 1 deletion packages/zenn-markdown-html/src/embed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { MarkdownOptions } from './types';

import { escapeHtml } from 'markdown-it/lib/common/utils';
import { extractYoutubeVideoParameters } from './utils/url-matcher';
import {
extractYoutubeVideoParameters,
isDocswellUrl,
} from './utils/url-matcher';
cm-igarashi-ryosuke marked this conversation as resolved.
Show resolved Hide resolved
import {
sanitizeEmbedToken,
generateEmbedServerIframe,
Expand All @@ -25,6 +28,7 @@ export type EmbedType =
| 'slideshare'
| 'speakerdeck'
| 'jsfiddle'
| 'docswell'
| 'codepen'
| 'codesandbox'
| 'stackblitz'
Expand Down Expand Up @@ -77,6 +81,12 @@ export const embedGenerators: Readonly<EmbedGeneratorList> = {
key
)}" scrolling="no" allowfullscreen allow="encrypted-media" loading="lazy"></iframe></span>`;
},
docswell(str) {
if (!isDocswellUrl(str)) {
return 'Docswellのembed用のURLを指定してください';
}
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>`;
},
jsfiddle(str) {
if (!isJsfiddleUrl(str)) {
return 'jsfiddleのURLが不正です';
Expand Down
4 changes: 4 additions & 0 deletions packages/zenn-markdown-html/src/utils/url-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function isJsfiddleUrl(url: string): boolean {
return /^(http|https):\/\/jsfiddle\.net\/[a-zA-Z0-9_,/-]+$/.test(url);
}

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

export function isYoutubeUrl(url: string): boolean {
return [
/^https?:\/\/youtu\.be\/[\w-]+(?:\?[\w=&-]+)?$/,
Expand Down
Loading