Skip to content

Commit

Permalink
Merge branch 'main' into sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope authored Jan 30, 2024
2 parents 97e36c1 + d666d1b commit d4a1931
Show file tree
Hide file tree
Showing 16 changed files with 872 additions and 75 deletions.
10 changes: 5 additions & 5 deletions e2e/docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ export default defineUserConfig({

sidebar: {
'/': ['/sidebar/'],
'/sidebar/': [
'/sidebar/heading/': 'heading',
'/sidebar/config/': [
{
text: 'Sidebar',
link: '/sidebar/',
link: '/sidebar/config/',
children: [
{ text: 'sidebar 1', link: '/sidebar/1.html' },
{ text: 'sidebar 2', link: '/sidebar/2.html' },
{ text: 'sidebar 1', link: '/sidebar/config/1.html' },
{ text: 'sidebar 2', link: '/sidebar/config/2.html' },
],
},
],
'/heading-sidebar/': 'heading',
},
}),
}) as UserConfig
7 changes: 0 additions & 7 deletions e2e/docs/heading-sidebar/README.md

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions e2e/docs/sidebar/heading/1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Sidebar 1

## Sidebar 1 Heading 1

## Sidebar 1 Heading 2
5 changes: 5 additions & 0 deletions e2e/docs/sidebar/heading/2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Sidebar 2

## Sidebar 2 Heading 1

## Sidebar 2 Heading 2
37 changes: 0 additions & 37 deletions e2e/tests/theme-default/composables/useSidebarItems.cy.ts

This file was deleted.

47 changes: 39 additions & 8 deletions e2e/tests/theme-default/sidebar.cy.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
it('has heading sidebar', () => {
cy.visit('/sidebar/')
cy.get('.theme-default-content').then((el) => {
cy.wrap(el)
.get('a.sidebar-item')
.should('contain', 'Sidebar Heading 1')
.should('contain', 'Sidebar Heading 2')
describe('has heading sidebar', () => {
it('frontmatter', () => {
cy.visit('/sidebar/auto.html')
cy.get('.theme-default-content').then((el) => {
cy.wrap(el)
.get('a.sidebar-item')
.should('contain', 'Sidebar Heading 1')
.should('contain', 'Sidebar Heading 2')
})
})

it('config', () => {
cy.visit('/sidebar/heading/1.html')

cy.get('.theme-default-content').then((el) => {
cy.wrap(el)
.get('h1')
.invoke('text')
.should('be.not.empty')
.then((text) => {
cy.get('.sidebar-heading')
.invoke('text')
.should('contain', text.replace('#', '').trim())
})

cy.wrap(el)
.get('h2')
.each((header) => {
cy.wrap(header)
.invoke('text')
.should('be.not.empty')
.then((headerText) => {
cy.get('a.sidebar-item').contains(
headerText.replace('#', '').trim(),
)
})
})
})
})
})

it('has configured sidebar', () => {
cy.visit('/sidebar/1.html')
cy.visit('/sidebar/config/1.html')
cy.get('.theme-default-content').then((el) => {
cy.wrap(el)
.get('a.sidebar-item')
Expand Down
1 change: 1 addition & 0 deletions tools/helper/src/node/page/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './excerpt.js'
export * from './text.js'
159 changes: 159 additions & 0 deletions tools/helper/src/node/page/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// eslint-disable-next-line vue/prefer-import-from-vue
import { isHTMLTag } from '@vue/shared'
import type { AnyNode } from 'cheerio'
import { load } from 'cheerio'
import type { App, Page } from 'vuepress/core'
import {} from 'vuepress/shared'
import { isArray } from '../../shared/index.js'

const MEDIA_WITH_ALT = ['img']

const REMOVED_TAGS = [
// non content
'title',
'base',
'meta',
'template',
'script',
'style',
'canvas',
'slot',

// not main content
'nav',
'aside',
'footer',

// deleted
'del',
's',

// rich media
'audio',
'video',
'canvas',
'iframe',
'map',
'area',
'track',
'object',

// input
'input',
'textarea',
'select',
'option',
'optgroup',
'datalist',
]

export interface PageTextOptions {
/**
* Whether convert text to single line content
*
* 是否将文字转换成单行内容
*
* @default false
*/
singleLine?: boolean

/**
* Length of text
*
* @description Text length will be the minimal possible length reaching this value
*
* 文字的长度
*
* @description 文字的长度会尽可能的接近这个值
*
* @default 300
*/
length?: number

/**
* Tags to be removed
*
* @description Table and code blocks are removed by default.
*
* 需要移除的标签
*
* @description 默认情况下表格和代码块会被移除
*
* @default ['table', 'pre']
*/
removedTags?: string[]
}

interface NodeOptions {
base: string
removedTags: string[]
}

const handleNode = (
node: AnyNode,
{ base, removedTags }: NodeOptions,
): string => {
if (node.type === 'tag') {
// toc should be dropped
if (
[node.attribs.class, node.attribs.id].some((item) =>
['table-of-contents', 'toc'].includes(item),
)
)
return ''

// return alt text
if (MEDIA_WITH_ALT.includes(node.tagName)) {
return node.attribs.alt || ''
}

// html tags can be returned
if (
!REMOVED_TAGS.includes(node.tagName) &&
!removedTags.includes(node.tagName) &&
isHTMLTag(node.tagName)
) {
return handleNodes(node.children, { base, removedTags })
}

return ''
}

if (node.type === 'text') return node.data

return ''
}

const handleNodes = (
nodes: AnyNode[] | null,
{ base, removedTags }: NodeOptions,
): string =>
isArray(nodes)
? nodes.map((node) => handleNode(node, { base, removedTags })).join('')
: ''

const $ = load('')

export const getPageText = (
{ options: { base } }: App,
{ contentRendered }: Page,
{
length = 300,
singleLine,
removedTags = ['table', 'pre'],
}: PageTextOptions = {},
): string => {
let result = ''
const rootNodes = $.parseHTML(contentRendered) || []

for (const node of rootNodes) {
const text = handleNode(node, { base, removedTags })

if (text) {
result += text
if (text.length >= length) break
}
}

return singleLine ? result.replace(/\n/g, ' ').replace(/\s+/g, ' ') : result
}
2 changes: 1 addition & 1 deletion tools/helper/tests/__fixtures__/src/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Classic:
| center | right | left |
| :------------------------: | -----------------------: | :---------------------- |
| For center align use `:-:` | For right align use `-:` | For left align use `:-` |
| b | aaaaaaaaa | aaaa |
| table text | aaaaaaaaa | aaaa |
| c | aaaa | a |

## Codes
Expand Down
Loading

0 comments on commit d4a1931

Please sign in to comment.