我正在使用 Express、PugJs 和 Prismic.io(无头 CMS)来创建博客文章。
Prismic 端点为文章的每个部分返回一个带有“类型”的 JSON 正文,即它可以是段落、图像、标题或列表。
然后我使用 pugjs 到 case 语句以下列方式处理每种类型:
div(class='article-body')
- let ul_list = false
- let ol_list = false
each item in document.ik_article_content
case item.type
when 'paragraph'
- ol_list = false
- ul_list = false
p #{item.text}
when 'heading1'
h1 #{item.text}
when 'heading2'
h2 #{item.text}
when 'heading3'
h3 #{item.text}
when 'heading4'
h4 #{item.text}
when 'image'
img(class='article-body__image' src=`${item.url}`)
when 'hyperlink'
a(href=`${item.text}`)
when 'o-list-item'
if !ol_list
- ol_list = true
ol
li #{item.text}
else
li #{item.text}
when 'list-item'
if !ul_list
- ul_list = true
ul
li #{item.text}
else
default
p #{item.text}
棱镜返回类型:'o-list-item'(有序列表)和'list-item'(无序列表)。
我需要解释这些类型以创建开始和结束或标签。
问题是我不确定如何做到这一点,尤其是使用自动关闭标签的 pugjs。
在上面的代码中,我尝试创建一个指示列表已开始的变量,然后如果列表已结束,我尝试将该变量设置为 false。但这不起作用。
我还能如何处理使用 pugjs 动态创建有序和无序列表?
谢谢你。