-
-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(blocks): notion html embed youtube support
- Loading branch information
1 parent
71f6dc0
commit b029b59
Showing
14 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
packages/affine/block-embed/src/common/adapters/notion-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
type BlockNotionHtmlAdapterMatcher, | ||
HastUtils, | ||
} from '@blocksuite/affine-shared/adapters'; | ||
import { nanoid } from '@blocksuite/store'; | ||
|
||
export function createEmbedBlockNotionHtmlAdapterMatcher( | ||
flavour: string, | ||
urlRegex: RegExp, | ||
{ | ||
toMatch = o => { | ||
const isFigure = | ||
HastUtils.isElement(o.node) && o.node.tagName === 'figure'; | ||
const embededFigureWrapper = HastUtils.querySelector(o.node, '.source'); | ||
if (!isFigure || !embededFigureWrapper) { | ||
return false; | ||
} | ||
const embededURL = HastUtils.querySelector(embededFigureWrapper, 'a') | ||
?.properties.href; | ||
return ( | ||
!!embededURL && | ||
typeof embededURL === 'string' && | ||
urlRegex.test(embededURL) | ||
); | ||
}, | ||
fromMatch = o => o.node.flavour === flavour, | ||
toBlockSnapshot = { | ||
enter: (o, context) => { | ||
if (!HastUtils.isElement(o.node)) { | ||
return; | ||
} | ||
const { assets, walkerContext } = context; | ||
if (!assets) { | ||
return; | ||
} | ||
|
||
const embededFigureWrapper = HastUtils.querySelector(o.node, '.source'); | ||
if (!embededFigureWrapper) { | ||
return; | ||
} | ||
|
||
let embededURL = ''; | ||
const embedA = HastUtils.querySelector(embededFigureWrapper, 'a'); | ||
embededURL = | ||
typeof embedA?.properties.href === 'string' | ||
? embedA.properties.href | ||
: ''; | ||
if (!embededURL) { | ||
return; | ||
} | ||
|
||
walkerContext | ||
.openNode( | ||
{ | ||
type: 'block', | ||
id: nanoid(), | ||
flavour, | ||
props: { | ||
url: embededURL, | ||
}, | ||
children: [], | ||
}, | ||
'children' | ||
) | ||
.closeNode(); | ||
walkerContext.skipAllChildren(); | ||
}, | ||
}, | ||
fromBlockSnapshot = {}, | ||
}: { | ||
toMatch?: BlockNotionHtmlAdapterMatcher['toMatch']; | ||
fromMatch?: BlockNotionHtmlAdapterMatcher['fromMatch']; | ||
toBlockSnapshot?: BlockNotionHtmlAdapterMatcher['toBlockSnapshot']; | ||
fromBlockSnapshot?: BlockNotionHtmlAdapterMatcher['fromBlockSnapshot']; | ||
} = Object.create(null) | ||
): BlockNotionHtmlAdapterMatcher { | ||
return { | ||
flavour, | ||
toMatch, | ||
fromMatch, | ||
toBlockSnapshot, | ||
fromBlockSnapshot, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/affine/block-embed/src/embed-figma-block/adapters/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './html.js'; | ||
export * from './markdown.js'; | ||
export * from './notion-html.js'; | ||
export * from './plain-text.js'; |
14 changes: 14 additions & 0 deletions
14
packages/affine/block-embed/src/embed-figma-block/adapters/notion-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { EmbedFigmaBlockSchema } from '@blocksuite/affine-model'; | ||
import { BlockNotionHtmlAdapterExtension } from '@blocksuite/affine-shared/adapters'; | ||
|
||
import { createEmbedBlockNotionHtmlAdapterMatcher } from '../../common/adapters/notion-html.js'; | ||
import { figmaUrlRegex } from '../embed-figma-model.js'; | ||
|
||
export const embedFigmaBlockNotionHtmlAdapterMatcher = | ||
createEmbedBlockNotionHtmlAdapterMatcher( | ||
EmbedFigmaBlockSchema.model.flavour, | ||
figmaUrlRegex | ||
); | ||
|
||
export const EmbedFigmaNotionHtmlAdapterExtension = | ||
BlockNotionHtmlAdapterExtension(embedFigmaBlockNotionHtmlAdapterMatcher); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/affine/block-embed/src/embed-github-block/adapters/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './html.js'; | ||
export * from './markdown.js'; | ||
export * from './notion-html.js'; | ||
export * from './plain-text.js'; |
14 changes: 14 additions & 0 deletions
14
packages/affine/block-embed/src/embed-github-block/adapters/notion-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { EmbedGithubBlockSchema } from '@blocksuite/affine-model'; | ||
import { BlockNotionHtmlAdapterExtension } from '@blocksuite/affine-shared/adapters'; | ||
|
||
import { createEmbedBlockNotionHtmlAdapterMatcher } from '../../common/adapters/notion-html.js'; | ||
import { githubUrlRegex } from '../embed-github-model.js'; | ||
|
||
export const embedGithubBlockNotionHtmlAdapterMatcher = | ||
createEmbedBlockNotionHtmlAdapterMatcher( | ||
EmbedGithubBlockSchema.model.flavour, | ||
githubUrlRegex | ||
); | ||
|
||
export const EmbedGithubNotionHtmlAdapterExtension = | ||
BlockNotionHtmlAdapterExtension(embedGithubBlockNotionHtmlAdapterMatcher); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/affine/block-embed/src/embed-loom-block/adapters/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './html.js'; | ||
export * from './markdown.js'; | ||
export * from './notion-html.js'; | ||
export * from './plain-text.js'; |
14 changes: 14 additions & 0 deletions
14
packages/affine/block-embed/src/embed-loom-block/adapters/notion-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { EmbedLoomBlockSchema } from '@blocksuite/affine-model'; | ||
import { BlockNotionHtmlAdapterExtension } from '@blocksuite/affine-shared/adapters'; | ||
|
||
import { createEmbedBlockNotionHtmlAdapterMatcher } from '../../common/adapters/notion-html.js'; | ||
import { loomUrlRegex } from '../embed-loom-model.js'; | ||
|
||
export const embedLoomBlockNotionHtmlAdapterMatcher = | ||
createEmbedBlockNotionHtmlAdapterMatcher( | ||
EmbedLoomBlockSchema.model.flavour, | ||
loomUrlRegex | ||
); | ||
|
||
export const EmbedLoomNotionHtmlAdapterExtension = | ||
BlockNotionHtmlAdapterExtension(embedLoomBlockNotionHtmlAdapterMatcher); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/affine/block-embed/src/embed-youtube-block/adapters/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './html.js'; | ||
export * from './markdown.js'; | ||
export * from './notion-html.js'; | ||
export * from './plain-text.js'; |
14 changes: 14 additions & 0 deletions
14
packages/affine/block-embed/src/embed-youtube-block/adapters/notion-html.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { EmbedYoutubeBlockSchema } from '@blocksuite/affine-model'; | ||
import { BlockNotionHtmlAdapterExtension } from '@blocksuite/affine-shared/adapters'; | ||
|
||
import { createEmbedBlockNotionHtmlAdapterMatcher } from '../../common/adapters/notion-html.js'; | ||
import { youtubeUrlRegex } from '../embed-youtube-model.js'; | ||
|
||
export const embedYoutubeBlockNotionHtmlAdapterMatcher = | ||
createEmbedBlockNotionHtmlAdapterMatcher( | ||
EmbedYoutubeBlockSchema.model.flavour, | ||
youtubeUrlRegex | ||
); | ||
|
||
export const EmbedYoutubeNotionHtmlAdapterExtension = | ||
BlockNotionHtmlAdapterExtension(embedYoutubeBlockNotionHtmlAdapterMatcher); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters