+
Skip to content

feat: extract rehypeConfluence #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-spies-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cf-cleaner": patch
---

feat: extract rehypeConfluence
5 changes: 5 additions & 0 deletions .changeset/wet-boxes-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'cf-cleaner': patch
---

feat: first blood, should just work
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs"
".": {
"import": "./lib/index.js",
"require": "./lib/index.cjs"
},
"./lib/rehype-confluence": "./lib/rehype-confluence.js"
},
"types": "lib",
"files": [
Expand Down
100 changes: 1 addition & 99 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,110 +1,12 @@
import { URL } from 'url'

import type { Element, Root } from 'hast'
import rehypeFormat from 'rehype-format'
import rehypeParse from 'rehype-parse'
import rehypePresetMinify from 'rehype-preset-minify'
import rehypeStringify from 'rehype-stringify'
import { unified } from 'unified'
import type { MinimalDuplex } from 'unified-stream'
import { stream } from 'unified-stream'
import { remove } from 'unist-util-remove'
import { visit } from 'unist-util-visit'

const CLASSNAME_MAPPER = {
'toc-macro': 'toc',
'hide-toolbar': 'expandable',
'confluence-information-macro': 'alert',
code: 'code',
panel: 'panel',
panelHeader: 'panel-header',
panelContent: 'panel-content',
confluenceTd: 'border-td',
confluenceTh: 'border-th',
}

// eslint-disable-next-line sonarjs/cognitive-complexity
export const rehypeConfluence = () => (root: Root) => {
remove(
root,
node =>
node.type === 'element' &&
(['style', 'script'].includes(node.tagName) ||
(node.properties?.className as string[] | undefined)?.some(className =>
['aui-icon', 'hide-border-bottom', 'hidden'].includes(className),
)),
)

remove(root, node => {
if (node.type === 'element' && node.tagName === 'p') {
return node.children.every(item => {
if (item.type === 'text') {
return !item.value.trim()
}

return false
})
}
})

visit(
root,
node => node.type === 'element' && !!(node as Element).properties,
_el => {
const properties = (_el as Element).properties!
for (const key of Object.keys(properties)) {
switch (key) {
case 'className': {
const className = properties.className as string[]
properties.className = className
// eslint-disable-next-line array-callback-return
.map(name => {
if (name in CLASSNAME_MAPPER) {
return CLASSNAME_MAPPER[name as keyof typeof CLASSNAME_MAPPER]
}

if (name.startsWith('confluence-information-macro-')) {
return name.replace('confluence-information-macro-', 'alert-')
}
})
.filter(Boolean) as string[]
if (properties.className.length === 0) {
delete properties.className
}
if (properties.style) {
delete properties.style
}
break
}
case 'dataSyntaxhighlighterParams': {
const params = properties[key] as string
const matched = /brush:\s*([^;]+);/.exec(params)
const lang = matched?.[1]
if (lang) {
properties.className = ['language-' + lang]
}
delete properties[key]
break
}
case 'href': {
const href = properties.href as string
const url = new URL(href)
const matched = /\?pageId=(\d+)/.exec(url.search)
if (matched) {
properties.href = '/knowledge/' + matched[1] + url.hash
}
break
}
default: {
if (key.startsWith('data')) {
delete properties[key]
}
}
}
}
},
)
}
import { rehypeConfluence } from './rehype-confluence'

const getProcessor = (minify?: boolean | undefined) => {
let processor = unified()
Expand Down
89 changes: 89 additions & 0 deletions src/rehype-confluence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { Element, Root } from 'hast'
import { remove } from 'unist-util-remove'
import { visit } from 'unist-util-visit'

const CLASSNAME_MAPPER = {
'toc-macro': 'toc',
'hide-toolbar': 'expandable',
'confluence-information-macro': 'alert',
code: 'code',
panel: 'panel',
panelHeader: 'panel-header',
panelContent: 'panel-content',
confluenceTd: 'border-td',
confluenceTh: 'border-th',
}

// eslint-disable-next-line sonarjs/cognitive-complexity
export const rehypeConfluence = () => (root: Root) => {
remove(
root,
node =>
node.type === 'element' &&
(['style', 'script'].includes(node.tagName) ||
(node.properties?.className as string[] | undefined)?.some(className =>
['aui-icon', 'hide-border-bottom', 'hidden'].includes(className),
)),
)

remove(root, node => {
if (node.type === 'element' && node.tagName === 'p') {
return node.children.every(item => {
if (item.type === 'text') {
return !item.value.trim()
}

return item.type === 'element' && item.tagName === 'br'
})
}
})

visit(
root,
node => node.type === 'element' && !!(node as Element).properties,
_el => {
const properties = (_el as Element).properties!
for (const key of Object.keys(properties)) {
switch (key) {
case 'className': {
const className = properties.className as string[]
properties.className = className
// eslint-disable-next-line array-callback-return
.map(name => {
if (name in CLASSNAME_MAPPER) {
return CLASSNAME_MAPPER[name as keyof typeof CLASSNAME_MAPPER]
}

if (name.startsWith('confluence-information-macro-')) {
return name.replace('confluence-information-macro-', 'alert-')
}
})
.filter(Boolean) as string[]
if (properties.className.length === 0) {
delete properties.className
}
if (properties.style) {
delete properties.style
}
break
}
case 'dataSyntaxhighlighterParams': {
const params = properties[key] as string
const matched = /brush:\s*([^;]+);/.exec(params)
const lang = matched?.[1]
if (lang) {
properties.className = ['language-' + lang]
}
delete properties[key]
break
}
default: {
if (key.startsWith('data')) {
delete properties[key]
}
}
}
}
},
)
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载