Docusaurus 文档侧边栏增加 New 标识

在使用 Docusaurus 搭建文档站点的时候,我们经常要给某个侧边栏菜单增加一些醒目的标识,比如针对新创建的文档给它一个 New 的标识, 以提醒过来看文档的用户这是一个新增加项或者新特性(阅读的时候不要遗漏)。

然而这个功能是 Docusaurus 本身没有的,但是它向我们提供了定制改造的支持。就是这个 Swizzling 特性。

有人可能还不太认识这个单词,看下它的翻译:

8112de9b193070fa88deac0b1b6b3594.png

说白了它的作用就是支持自定义布局样式

我们安装 Docusaurus 的时候根目录 src 里面并没有 theme 目录,当我们通过终端命令 npm run swizzle 会创建一个 theme 目录来放主题结构和样式代码。我们可以根据自己的需求对里面的 代码进行改造。

在这里我们需要找到文档侧边栏菜单功能所在的组件,可以通过 npm run swizzle -- --list 来查询都有哪些组件支持 Swizzle 操作。

从命名不难发现 DocSidebarItem 就是渲染侧边栏的菜单每一项的组件。

执行命令:

npm run swizzle @docusaurus/theme-classic DocSidebarItem -- --eject

执行完命令,目录 src/theme 下会多出一个 DocSidebarItem 目录,找到相关代码:

export default function DocSidebarItemCategory({
  item,
  onItemClick,
  activePath,
  level,
  index,
  ...props
}) {
  const {items, label, collapsible, className, href, customProps} = item;
  //...
  return (
    <li
      //...
      <div
        className={clsx('menu__list-item-collapsible', {
          'menu__list-item-collapsible--active': isCurrentPage,
        })}>
        <Link
          //...
          aria-current={isCurrentPage ? 'page' : undefined}
          aria-expanded={collapsible ? !collapsed : undefined}
          href={collapsible ? hrefWithSSRFallback ?? '#' : hrefWithSSRFallback}
          {...props}>
          {label}{ customProps && customProps.featured ? <sup className="new">New</sup> : '' }
        </Link>
      </div>
    </li>
  );
}

这里滤除无关的代码,直接定位到显示菜单名称的地方。

这里还有个问题就是,如果可以针对某一个菜单进行配置是否显示这个 New 的标识就好了。然而这个 Docusaurus 的作者也替你想好了。侧边栏项支持 customProps 属性。你可以添加自己需要的额外的字段。

topicSidebar: [
    'topic/intro',
    {
      type: 'category',
      label: 'Next.js',
      collapsible: false,
      items: [
        {
          type: 'autogenerated',
          dirName: 'topic/nextjs',
        }
      ],
      customProps: {
        featured: true
      }
    }
  ]

好了接下来,就只用给它定义样式了,参考 MDN 上的 BETA 标识:

3293f50da7bf70962bc0fea7dce3fd46.png
sup.new {
  align-self: flex-start;
  text-transform: uppercase;
  font-size: 0.45rem;
  border-radius: 1rem;
  color: #fff;
  background-color: #fa383e;
  text-rendering: optimizeLegibility;
  padding: 0 0.4em;
  line-height: 1.7;
  display: inline-block;
}

这里其它的 CSS 属性都好理解,就是 text-rendering 属性好像没怎么见过。特意查了一下 MDN 文档

text-rendering CSS 属性定义浏览器渲染引擎如何渲染字体。浏览器会在速度、清晰度、几何精度之间进行权衡。

一个视觉上很明显的效果是,optimizeLegibility 属性值会在某些字体(比如,微软的 Calibri、Candara、Constantia 和 Corbel,或者 DejaVu 系列字体)小于 20px 时 把某些相邻字符连接起来(比如 ff、fi、fl 等)。

/* Keyword values */
text-rendering: auto;
text-rendering: optimizeSpeed;
text-rendering: optimizeLegibility;
text-rendering: geometricPrecision;

/* Global values */
text-rendering: inherit;
text-rendering: initial;
text-rendering: revert;
text-rendering: revert-layer;
text-rendering: unset;

下面看下这几个值所代表的含义:

  • auto
    浏览器依照某些根据去推测在绘制文本时,何时该优化速度,易读性或者几何精度。对于该值在不同浏览器中解释的差异,请看兼容性表。

  • optimizeSpeed
    浏览器在绘制文本时将着重考虑渲染速度,而不是易读性和几何精度。它会使字间距和连字无效。

  • optimizeLegibility
    浏览器在绘制文本时将着重考虑易读性,而不是渲染速度和几何精度。它会使字间距和连字有效。该属性值在移动设备上会造成比较明显的性能问题

  • geometricPrecision
    浏览器在绘制文本时将着重考虑几何精度,而不是渲染速度和易读性。字体的某些方面—比如字间距—不再线性缩放,所以该值可以使使用某些字体的文本看起来不错。

我们这里为了清楚地显示 New 字样,将它的值设置为 optimizeLegibility

下面就是最终实现的效果:

c0749a40b6d4816140fc8b59e1e4853d.png

后面如果哪个侧边栏菜单项需要增加 New 标识,只需要在侧边栏配置文件 sidebars.js 对应的菜单配置属性 customProps 里面 增加 featured: true 即可。

这篇就写到这里,谢谢你的阅读~

  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 中使用 Markdown 生成文档时,可以通过以下步骤自动生成侧边目录: 1. 在项目中安装 `markdown-it` 和 `markdown-it-anchor` 库: ```bash npm install markdown-it markdown-it-anchor --save ``` 2. 在 Vue 组件中引入并初始化 `markdown-it` 和 `markdown-it-anchor`: ```javascript import MarkdownIt from 'markdown-it' import MarkdownItAnchor from 'markdown-it-anchor' const md = new MarkdownIt({ html: true, linkify: true, typographer: true, }) md.use(MarkdownItAnchor) ``` 3. 在 Vue 组件中使用 `v-html` 指令渲染 Markdown 文本,并使用 `@hook:mounted` 钩子函数监听渲染完成事件,在事件处理函数中获取文档中所有标题标签(如 `h1`、`h2` 等),并根据标题级别生成目录树: ```html <template> <div> <div v-html="html"></div> <div class="sidebar"> <ul> <li v-for="item in toc" :key="item.id"> <a :href="'#' + item.id">{{ item.text }}</a> <ul v-if="item.children"> <li v-for="child in item.children" :key="child.id"> <a :href="'#' + child.id">{{ child.text }}</a> </li> </ul> </li> </ul> </div> </div> </template> <script> export default { data() { return { html: '', toc: [], } }, mounted() { this.html = md.render('# Hello World\n## Introduction\n### Features\n### Installation\n## Usage\n## API Reference') this.$nextTick(() => { const headers = Array.from(document.querySelectorAll('h1, h2, h3, h4, h5, h6')) this.toc = headers.map(header => ({ id: header.id, text: header.textContent, level: Number(header.tagName[1]), })).reduce((acc, header) => { let lastItem = acc[acc.length - 1] if (!lastItem || header.level <= lastItem.level) { acc.push(header) } else { let parent = lastItem while (parent.children && header.level > parent.level) { parent = parent.children[parent.children.length - 1] } if (!parent.children) { parent.children = [] } parent.children.push(header) } return acc }, []) }) }, } </script> ``` 4. 在 Vue 组件的样式中定义侧边样式: ```css .sidebar { position: fixed; top: 0; right: 0; bottom: 0; width: 200px; padding: 20px; overflow-y: auto; } ``` 这样就可以在 Vue Markdown 文档中自动生成侧边目录了。当然,这只是一个简单的示例,实际项目中可能需要根据具体需求进行一些定制化调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值