Skip to content

Commit

Permalink
type: Fix type error caused by upgrade unified v10 #13
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 15, 2021
1 parent 4a1cd8b commit e60070c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 54 deletions.
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,27 @@
"unified"
],
"jest": {
"testMatch": [
"<rootDir>/test/*.{ts,tsx}"
],
"collectCoverageFrom": [
"lib/*.js",
"!lib/*.d.ts"
"<rootDir>/src/*.{tsx,ts}"
],
"transformIgnorePatterns": [
"node_modules/(?!(unified|bail|is-plain-obj|trough|vfile|vfile-message|unist-util-stringify-position)/)"
]
},
"dependencies": {
"ts-mdast": "1.0.0",
"unified": "10.0.0"
},
"devDependencies": {
"@types/jest": "26.0.23",
"jest": "27.0.3",
"@types/jest": "26.0.24",
"rehype": "11.0.0",
"rehype-raw": "5.1.0",
"rehype-stringify": "8.0.0",
"remark-gfm": "1.0.0",
"remark-parse": "9.0.0",
"remark-rehype": "8.1.0",
"tsbb": "2.2.0"
"tsbb": "2.2.1"
}
}
21 changes: 11 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Root, Parent } from 'ts-mdast'
import {Plugin} from 'unified'
import { Plugin, Transformer } from 'unified'
import { Parent, NodeData, Node } from 'unist';
import visit from './visit'
import { propertiesHandle, nextChild, prevChild, getCommentObject } from './utils'
import { propertiesHandle, nextChild, prevChild, getCommentObject } from './utils';

export type MdastTransformer = (tree: Root) => void
export type MdastTransformer = (tree: NodeData<Parent>) => void

export type RehypeAttrsOptions = {
/**
Expand Down Expand Up @@ -51,12 +51,13 @@ const defaultOptions: RehypeAttrsOptions = {
properties: 'data'
}

const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): MdastTransformer =>{
const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): Transformer => {
const opts = { ...defaultOptions, ...options }
return transformer
function transformer(tree: Root) {
visit(tree, 'element', (node: Root, index: number, parent: Parent) => {
const codeNode = node && node.children && node.children[0] as any
return transformer;
function transformer(tree: Node<NodeData<Parent>>): void {
// ????? any
visit(tree as any, 'element', (node: NodeData<Parent>, index: number, parent: NodeData<Parent>) => {
const codeNode = node && node.children && Array.isArray(node.children) && node.children[0]
if (node.tagName === 'pre' && codeNode && codeNode.tagName === 'code' && Array.isArray(parent.children) && parent.children.length > 1) {
const child = prevChild(parent.children, index)
if (child) {
Expand All @@ -67,7 +68,7 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): MdastTransformer =
}
}
}
if (/^(em|strong|b|a|i|p|pre|blockquote|h(1|2|3|4|5|6)|code|table|img|del|ul|ol)$/.test(node.tagName as string)) {
if (/^(em|strong|b|a|i|p|pre|blockquote|h(1|2|3|4|5|6)|code|table|img|del|ul|ol)$/.test(node.tagName as string) && Array.isArray(parent.children)) {
const child = nextChild(parent.children, index)
if (child) {
const attr = getCommentObject(child)
Expand Down
10 changes: 5 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Content } from 'ts-mdast'
import { Parent, NodeData } from 'unist';
import { RehypeAttrsOptions } from './'

export const getURLParameters = (url: string): Record<string, string | number | boolean> =>
Expand All @@ -11,11 +11,11 @@ export const getURLParameters = (url: string): Record<string, string | number |
);

type CommentData = {
type: 'comment',
type?: 'comment',
value?: string,
}

export const prevChild = (data: Content[] = [], index: number): CommentData | undefined => {
export const prevChild = (data: NodeData<Parent>[] = [], index: number): CommentData | undefined => {
let i = index;
while (i > -1) {
i--;
Expand All @@ -28,7 +28,7 @@ export const prevChild = (data: Content[] = [], index: number): CommentData | un
return;
}

export const nextChild = (data: Content[] = [], index: number, tagName?: string): CommentData | undefined => {
export const nextChild = (data: NodeData<Parent>[] = [], index: number, tagName?: string): CommentData | undefined => {
let i = index;
while (i < data.length) {
i++;
Expand Down Expand Up @@ -71,7 +71,7 @@ export const getCommentObject = ({ value = '' }: CommentData): Record<string, st
return param;
}

export const propertiesHandle = (defaultAttrs: Record<string, string>, attrs: Record<string, string | number | boolean | null>, type: RehypeAttrsOptions['properties']) => {
export const propertiesHandle = (defaultAttrs?: Record<string, string> | null, attrs?: Record<string, string | number | boolean | null> | null, type?: RehypeAttrsOptions['properties']) => {
if (type === 'string') {
return { ...defaultAttrs, 'data-config': JSON.stringify({ ...attrs, rehyp: true })}
} else if (type === 'attr') {
Expand Down
9 changes: 4 additions & 5 deletions src/visit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Root, Parent, Content } from 'ts-mdast'
import { Parent, NodeData } from 'unist';

export type VisitCallback = (node: Root | Content | Parent, index: number, parent: Parent | Content) => void

export default function visit(tree: Root, element: string, callback: VisitCallback) {
export type VisitCallback = (node: NodeData<Parent>, index: number, parent: NodeData<Parent>) => void;
export default function visit(tree?: NodeData<Parent>, element?: string, callback?: VisitCallback) {
if (!element || !tree || !callback || typeof callback !== 'function') {
return
}
Expand All @@ -11,7 +10,7 @@ export default function visit(tree: Root, element: string, callback: VisitCallba
}
}

function handle(tree: Content[], element: string, parent: Parent | Content, callback: VisitCallback) {
function handle(tree: NodeData<Parent>[], element: string, parent: NodeData<Parent>, callback: VisitCallback) {
tree.forEach((item, index) => {
if (item.type === element) {
callback(item, index, parent)
Expand Down
37 changes: 20 additions & 17 deletions __tests__/index.test.ts → test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/// <reference types="jest" />

const unified = require("unified");
const rehype = require('rehype')
const stringify = require('rehype-stringify')
const rehypeRaw = require('rehype-raw')
const remark2rehype = require('remark-rehype')
const remarkParse = require('remark-parse')
const gfm = require('remark-gfm')
const rehypeAttrs = require('../lib')
const utils = require('../lib/utils')
const visit = require('../lib/visit')
import { unified, Plugin } from 'unified'
import { Parent, NodeData } from 'unist';
import rehype from 'rehype';
import gfm from 'remark-gfm';
import rehypeRaw from 'rehype-raw';
import remark2rehype from 'remark-rehype';
import remarkParse from 'remark-parse';
import stringify from 'rehype-stringify';
import rehypeAttrs from '../src';
import * as utils from '../src/utils';
import visit from '../src/visit';

const mrkStr = "<!--rehype:title=Rehype Attrs-->\n```js\nconsole.log('')\n```"

describe('rehype-attr function test case', () => {
it('visit', async () => {
const node = {
const node: NodeData<Parent> = {
"type": "root",
"children": [
{
Expand All @@ -31,9 +30,9 @@ describe('rehype-attr function test case', () => {
],
"data": { "quirksMode": false }
}
visit(node, 'element', (node, index, parent) => {
expect(/(del|p)/.test(node.tagName)).toBeTruthy()
expect(typeof node).toEqual('object')
visit(node, 'element', (childNode, index, parent) => {
expect(/(del|p)/.test((childNode as any).tagName)).toBeTruthy()
expect(typeof childNode).toEqual('object')
expect(typeof index).toEqual('number')
expect(typeof parent).toEqual('object')
})
Expand Down Expand Up @@ -387,9 +386,13 @@ describe('rehype-attr test case', () => {
}
].forEach((data, idx) => {
it(data.title, async () => {
const pluginWithoutOptions: Plugin<void[]> = (options) => {
// expectType<void>(options)
}

const htmlStr = rehype()
.data('settings', { fragment: true })
.use(rehypeAttrs, { properties: 'attr' })
.use(rehypeAttrs as any, { properties: 'attr' })
.processSync(data.markdown)
.toString()
expect(htmlStr).toEqual(data.expected);
Expand Down
39 changes: 28 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
{
"compilerOptions": {
"jsx": "react",
"target": "esnext",
"module": "commonjs",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": true,
"declaration": true,
"baseUrl": ".",
"noFallthroughCasesInSwitch": true,
"noImplicitThis": true,
"strictBindCallApply": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"stripInternal": true,
"noEmit": true,
"types": ["jest", "node"],
// "types": ["jest", "node"],


"esModuleInterop": false,
"noImplicitAny": true,
"outDir": "lib",
"baseUrl": "."

// // "module": "commonjs",
// "noImplicitThis": true,
// "strictBindCallApply": true,
// "strictNullChecks": true,
// "strictPropertyInitialization": true,
// "stripInternal": true,
// "noImplicitAny": true,
},
"include": [
"src/**/*", ]
"src/types/*",
"src",
"test"
]
}

0 comments on commit e60070c

Please sign in to comment.