Skip to content

Commit

Permalink
All: Reduce dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Oct 15, 2023
1 parent eac081c commit 634f7d3
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 25 deletions.
6 changes: 6 additions & 0 deletions .changeset/calm-walls-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'next-docs-zeta': patch
'next-docs-ui': patch
---

Reduce dependencies
2 changes: 1 addition & 1 deletion packages/next-docs-ui/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineConfig({
'./src/components/dialog/{search,search-default,search-algolia}.tsx',
'./src/*.{ts,tsx}'
],
external: ['next-docs-zeta', 'shiki', 'algoliasearch'],
external: ['next-docs-zeta'],
format: 'esm',
dts: true,
target: tsconfig.compilerOptions.target as 'es2016'
Expand Down
4 changes: 2 additions & 2 deletions packages/next-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
"@formatjs/intl-localematcher": "^0.4.2",
"flexsearch": "0.7.21",
"github-slugger": "^2.0.0",
"hast-util-to-string": "^3.0.0",
"negotiator": "^0.6.3",
"react-remove-scroll": "^2.5.6",
"rehype-img-size": "^1.0.1",
Expand All @@ -135,7 +134,8 @@
"@types/react-dom": "18.2.1",
"algoliasearch": "^4.20.0",
"contentlayer": "^0.3.4",
"next": "13.5.4"
"next": "13.5.4",
"unified": "^10.1.2"
},
"keywords": [
"NextJs",
Expand Down
3 changes: 1 addition & 2 deletions packages/next-docs/src/mdx-plugins/rehype-next-docs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Slugger from 'github-slugger'
import { toString as flattenNode } from 'hast-util-to-string'
import rehypePrettycode from 'rehype-pretty-code'
import { visit } from './utils'
import { flattenNode, visit } from './utils'

const slugger = new Slugger()
const headings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import fs from 'fs'
import path from 'path'
import type { Plugin } from 'unified'
import { visit } from 'unist-util-visit'
import type { Plugin } from './types'

const regex = /^\|reference:(.+)\|/

Expand Down
2 changes: 1 addition & 1 deletion packages/next-docs/src/mdx-plugins/search-structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Slugger from 'github-slugger'
import { remark } from 'remark'
import remarkGfm from 'remark-gfm'
import remarkMdx from 'remark-mdx'
import type { Plugin } from 'unified'
import { visit } from 'unist-util-visit'
import type { Plugin } from './types'

type Heading = {
id: string
Expand Down
2 changes: 0 additions & 2 deletions packages/next-docs/src/mdx-plugins/types.d.ts

This file was deleted.

63 changes: 63 additions & 0 deletions packages/next-docs/src/mdx-plugins/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* @typedef {import('hast').Nodes} Nodes
* @typedef {import('hast').Parents} Parents
*/

/**
* Visit a node with filtered tag names
*
* @param {Nodes} node
* Node to visit.
* @param {Array<string>} tagNames
* @param {(node: Nodes) => void} handler
*/
export function visit(node, tagNames, handler) {
if (tagNames.includes(node.tagName)) {
handler(node)
Expand All @@ -6,3 +19,53 @@ export function visit(node, tagNames, handler) {

node.children?.forEach(n => visit(n, tagNames, handler))
}

/**
* Get the plain-text value of a hast node.
*
* @param {Nodes} node
* Node to serialize.
* @returns {string}
* Serialized node.
*/
export function flattenNode(node) {
// “The concatenation of data of all the Text node descendants of the context
// object, in tree order.”
if ('children' in node) {
return all(node)
}

// “Context object’s data.”
return 'value' in node ? node.value : ''
}

/**
* @param {Nodes} node
* Node.
* @returns {string}
* Serialized node.
*/
function one(node) {
if (node.type === 'text') {
return node.value
}

return 'children' in node ? all(node) : ''
}

/**
* @param {Parents} node
* Node.
* @returns {string}
* Serialized node.
*/
function all(node) {
/** @type {Array<string>} */
const result = []

for (let i = 0; i < node.children.length; i++) {
result[i] = one(node.children[i])
}

return result.join('')
}
2 changes: 1 addition & 1 deletion packages/next-docs/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig({
'src/{server,breadcrumb,sidebar,toc,search,link,contentlayer,middleware,mdx-plugins,algolia,search-algolia}/index.{ts,tsx}',
'src/contentlayer/configuration.ts'
],
external: ['contentlayer', 'algoliasearch'],
external: ['contentlayer'],
format: 'esm',
dts: true,
target: tsconfig.compilerOptions.target as 'es2016'
Expand Down
18 changes: 3 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 634f7d3

Please sign in to comment.