Notion Flow 모듈을 사용하여 변환하는 방법

✍🏼 작성일 2024년 03월 31일    💡 수정일 2024년 04월 01일
❗️ 참고: 이 글이 작성된 지 이미 일이 지났습니다. 시의성에 유의하세요
🖥  설명:저만의 특별한 Notion Flow 사용 팁이 있어요~

일주일 전, 저는 Notion Flow 브라우저 확장 프로그램을 구축했습니다:

Xheldon on Twitter / X Notion Flow 브라우저 확장이 드디어 출시되었습니다. 설정 및 소개는 이 동영상을 참조하세요: https://t.co/pw4yYwnt8h官网见:https://t.co/6326Q4gIWC插件用来将 Notion 내용을 Markdown이나 사용자 정의 형식으로 Github에 전송합니다. 저는 이를 Github Pages를 사용하는 Jekyll 블로그 작성에 활용하고 있으며, 확장 프로그램은 OSS 설정 후 자동으로 이미지 내용을 CDN으로 처리해줍니다. 정말 유용해요~. — Xheldon (@\_Xheldon) 2024년 3월 20일 https://twitter.com/_Xheldon/status/1770466495560294583

방금 업데이트된 0.4.1 버전:

0.4.1 | Notion Flow 기능 https://notion-flow.xheldon.com/blog/2024/03/31/0.4.1

AWS S3 API와 호환되는 자체 구축 OSS 서비스(예: Cloudflare R2)를 지원합니다:

Cloudflare R2 | 零出口费用分布式对象存储 | Cloudflare | Cloudflare Cloudflare R2는 S3 호환, 제로 이그레스 비용의 글로벌 분산 객체 스토리지입니다. 데이터를 자유롭게 이동시키고 원하는 멀티클라우드 아키텍처를 구축하세요. https://www.cloudflare.com/zh-cn/developer-platform/r2/

이 글에서는 제가 어떻게 이 브라우저 확장을 Github Jekyll 블로그에 활용하고 있는지 간단히 소개하겠습니다.


Jekyll 정적 블로그는 Ruby 기반으로 구축되었으며 플러그인을 지원합니다. 그래서 저는 Liquid 템플릿 언어를 처리하기 위해 몇 가지 플러그인을 직접 작성했습니다(Jekyll 블로그의 플러그인은 _plugins 디렉토리에 위치하며, ruby 파일을 작성한 후 해당 디렉토리에 넣고 서비스를 재시작하면 됩니다). 내용은 Notion Flow로 변환된 Notion 콘텐츠에서 가져옵니다. 예를 들어 북마크를 처리하는 플러그인 내용은 다음과 같습니다:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Jekyll
class RenderBookMarkBlock < Liquid::Block
def initialize(tag_name, attr, tokens)
super
# 普通的链接没有 yid 和 bid
attrs = attr.scan(/url\=\"(.*)\"\stitle\=\"(.*)\"\simg\=\"(.*)\"\syid\=\"(.*)\"\sbid\=\"(.*)\"/)
if !attrs.empty?
# 外部的 video 链接,youtube、bilibili(如本文上一篇博客就是)
@url = attrs[0][0]
@title = attrs[0][1]
@img = attrs[0][2]
@yid = attrs[0][3]
@bid = attrs[0][4]
@firstChar = (@title)[0].upcase
@error = ""
else
# 正常和 notion 一样的 bookmark(如本文上面三个链接就是)
attrs = attr.scan(/url\=\"(.*)\"\stitle\=\"(.*)\"\simg\=\"(.*)\"/)
@url = attrs[0][0]
@title = attrs[0][1]
@img = attrs[0][2]
@firstChar = (@title)[0].upcase
@error = ""
end
end
def render(context)
@desc = super
if !@yid.nil? && !@yid.empty?
"<p class='embed-responsive embed-responsive-16by9'><iframe src='https://www.youtube.com/embed/#{@yid}?rel=0' title='YouTube video player' frameborder='0' allow='accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe></p>"
elsif !@bid.nil? && !@bid.empty?
"<p class='embed-responsive embed-responsive-16by9' style='border-bottom: 1px solid #ddd;'><iframe src='//player.bilibili.com/player.html?bvid=#{@bid}&high_quality=1&as_wide=1' scrolling='no' border='0' frameborder='no' framespacing='0' allowfullscreen></iframe></p>"
else
"<p><a class='link-bookmark' href='#{@url}' target='_blank'><span data-bookmark-img='#{@img}' data-bookmark-title='#{@firstChar}'><img src='#{@img}'/></span><span><span>#{@title}</span><span>#{@desc}</span><span>#{@url}</span></span></a></p>"
end
end
end
end

# 上传的 video
module Jekyll
class RenderVideoBlock < Liquid::Block
def initialize(tag_name, attr, tokens)
super
attrs = attr.scan(/caption\=\"(.*)\"\simg\=\"(.*)\"\ssuffix\=\"(.*)\"/)
@caption = attrs[0][0]
@img = attrs[0][1]
@suffix = attrs[0][2]
end
def render(context)
text = super
"<p caption='#{@caption}'><video controls muted><source src='#{@img}' type='video/#{@suffix}' /></video></p>"
end
end
end

Liquid::Template.register_tag('render_bookmark', Jekyll::RenderBookMarkBlock)
Liquid::Template.register_tag('render_video', Jekyll::RenderVideoBlock)

이 코드의 로직은 Notion의 북마크 모듈 링크가 Youtube나 Bilibili인 경우 임베드 비디오 HTML(iframe)로 변환하고, 그렇지 않으면 Notion 북마크와 유사한 HTML로 변환하는 것입니다(CSS와 함께 구현 필요).

따라서 저는 Notion Flow를 사용해 Notion 내용을 Markdown 형식으로 변환하는 동시에 북마크 등 모듈의 변환 규칙을 커스터마이징하여 블로그에서 Youtube, Bilibili 및 Notion과 같은 북마크 스타일 콘텐츠를 표시할 수 있도록 했습니다. 예시는 다음과 같습니다:

1
2
3
4
5
6
7
8
9
10
11
12
13
video: function video(block) {
if (block.type === 'file') {
// 用户自己上传的 video 文件,用默认 video 插件处理
return `{% render_video caption="${block.caption}" img="${block.url}" suffix="${block.suffix}" %}\n![${block.caption}](${block.url})\n{% endrender_video %}\n`;
} else if (block.type === 'external') {
// 外部链接、youtube 和 bilibili 视频链接,用 bookmark 处理
return `{% render_bookmark url="${block.url}" title="${
block.caption || ''
}" img="" yid="${block.yid}" bid="${
block.bid
}" %}{% endrender_bookmark %}\n`;
}
}

여기서 주의할 점은(저는 ruby를 잘 모릅니다) Liquid 템플릿의 태그 사이에는 반드시 텍스트 내용이 있어야 합니다(사용하지 않아도 됨). 그렇지 않으면 ruby 플러그인이 HTML을 생성할 수 없습니다. 즉:

1
{% render_video  %}这里必须有任意内容!{% endrender_video %}

루비 플러그인에서 super 변수는 "여기에는 반드시 임의의 내용이 있어야 합니다!"라는 문장을 가져옵니다(해당 변수를 사용하지 않아도 됩니다). 이 내용이 없으면 플러그인은 아무것도 반환하지 않습니다.

- EOF -
이 글의 최초 게시: Notion Flow 모듈을 사용하여 변환하는 방법 - Xheldon Blog