Skip to content

Commit

Permalink
Mastodonの埋め込みを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
karasugawasu committed Jan 27, 2024
1 parent c987285 commit a798aef
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/shared/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const textcolorConfig = {
return `<div class="text-${color}">`;
} else {
// closing tag
return "</div>";
return "</div>\n";
}
},
};
Expand All @@ -110,7 +110,7 @@ const bgcolorConfig = {
return `<div class="bg-${color}">`;
} else {
// closing tag
return "</div>";
return "</div>\n";
}
},
};
Expand All @@ -132,6 +132,24 @@ const alertConfig = {
},
};

const mastodonEmbedConfig = {
validate: (params: string) => {
return params.trim().match(/^mdembed\s+(.*)$/);
},

render: (tokens: any, idx: any) => {
const m = tokens[idx].info.trim().match(/^mdembed\s+(.*)$/);
if (tokens[idx].nesting === 1) {
const url = mdToHtmlInline(md.utils.escapeHtml(m[1])).__html;
const domain = new URL(url).origin;
return `<div class="mastodon-embed"><iframe src="${url}/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script src="${domain}/embed.js" async="async"></script>`;
} else {
// closing tag
return "</div>\n";
}
},
};

const html5EmbedConfig = {
html5embed: {
useImageSyntax: true, // Enables video/audio embed with ![]() syntax (default)
Expand Down Expand Up @@ -277,6 +295,7 @@ export function setupMarkdown() {
.use(markdown_it_container, "color", textcolorConfig)
.use(markdown_it_container, "bgcolor", bgcolorConfig)
.use(markdown_it_container, "alert", alertConfig)
.use(markdown_it_container, "mdembed", mastodonEmbedConfig)
.use(markdown_it_highlightjs, { inline: true })
.use(markdown_it_ruby)
.use(localInstanceLinkParser)
Expand Down

0 comments on commit a798aef

Please sign in to comment.