Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jaywcjlove/rehype-rewrite
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.2
Choose a base ref
...
head repository: jaywcjlove/rehype-rewrite
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.0.0
Choose a head ref
  • 5 commits
  • 6 files changed
  • 2 contributors

Commits on Aug 27, 2021

  1. Verified

    This commit was signed with the committer’s verified signature.
    IvanGoncharov Ivan Goncharov
    Copy the full SHA
    66593b3 View commit details
  2. released v1.1.0

    jaywcjlove committed Aug 27, 2021
    Copy the full SHA
    ff63e5c View commit details

Commits on Sep 1, 2021

  1. doc: Update README.md

    jaywcjlove authored Sep 1, 2021
    Copy the full SHA
    1c2a3cc View commit details
  2. feat: Use ESM.

    jaywcjlove committed Sep 1, 2021
    Copy the full SHA
    beb7d23 View commit details
  3. released v2.0.0

    jaywcjlove committed Sep 1, 2021
    Copy the full SHA
    b0c33e4 View commit details
Showing with 67 additions and 64 deletions.
  1. +20 −18 README.md
  2. +11 −12 __tests__/index.test.ts
  3. +17 −16 package.json
  4. +8 −8 src/index.ts
  5. +7 −6 src/visit.ts
  6. +4 −4 tsconfig.json
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
rehype-rewrite
---

[![Downloads](https://img.shields.io/npm/dm/rehype-rewrite.svg?style=flat)](https://www.npmjs.com/package/rehype-rewrite)
[![NPM version](https://img.shields.io/npm/v/rehype-rewrite.svg?style=flat)](https://npmjs.org/package/rehype-rewrite)
[![Build](https://github.com/jaywcjlove/rehype-rewrite/actions/workflows/ci.yml/badge.svg)](https://github.com/jaywcjlove/rehype-rewrite/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/jaywcjlove/rehype-rewrite/badge.svg?branch=main)](https://coveralls.io/github/jaywcjlove/rehype-rewrite?branch=main)
@@ -9,17 +10,18 @@ Rewrite element with [rehype](https://github.com/rehypejs/rehype).

## Installation

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): Node 12+ is needed to use it and it must be `import` instead of `require`.

```bash
npm i rehype-rewrite
yarn add rehype-rewrite
npm install rehype-rewrite
```

## Usage

```js
const rehype = require('rehype')
const rehypeRewrite = require('rehype-rewrite')
const stringify = require('rehype-stringify')
import { rehype } from 'rehype';
import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';

const html = `<h1>header</h1>`;
const htmlStr = rehype()
@@ -46,9 +48,9 @@ const htmlStr = rehype()
```js
const rehype = require('rehype')
const rehypeRewrite = require('rehype-rewrite')
const stringify = require('rehype-stringify')
import { rehype } from 'rehype';
import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';
const html = `<h1>header</h1>`;
const htmlStr = rehype()
@@ -73,9 +75,9 @@ const htmlStr = rehype()
>
```js
const rehype = require('rehype')
const rehypeRewrite = require('rehype-rewrite')
const stringify = require('rehype-stringify')
import { rehype } from 'rehype';
import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';
const html = `<h1>hello</h1>`;
const htmlStr = rehype()
@@ -108,12 +110,12 @@ const htmlStr = rehype()
>
```js
const unified = require("unified");
const remarkParse = require('remark-parse')
const remark2rehype = require('remark-rehype')
const rehypeRaw = require('rehype-raw')
const rehypeRewrite = require('rehype-rewrite')
const stringify = require('rehype-stringify')
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import rehypeRaw from 'rehype-raw';
import remark2rehype from 'remark-rehype';
import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';
const html = `<h1>hello</h1>`;
const htmlStr = unified()
@@ -152,4 +154,4 @@ const htmlStr = unified()
## License
MIT © [Kenny Wong](https://github.com/jaywcjlove)
MIT © [Kenny Wong](https://github.com/jaywcjlove)
23 changes: 11 additions & 12 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/// <reference types="jest" />

const unified = require("unified");
const remarkParse = require('remark-parse')
const remark2rehype = require('remark-rehype')
const rehype = require('rehype')
const rehypeRaw = require('rehype-raw')
const stringify = require('rehype-stringify')
const rehypeRewrite = require('../lib')
import { unified } from 'unified'
import remarkParse from 'remark-parse';
import remark2rehype from 'remark-rehype';
import { rehype } from 'rehype';
import rehypeRaw from 'rehype-raw';
import stringify from 'rehype-stringify';
import rehypeRewrite from '../';

describe('rehype-rewrite test case', () => {
it('remove text', async () => {
@@ -30,7 +29,7 @@ describe('rehype-rewrite test case', () => {
const html = `<h1>header</h1>`;
const expected = `<html><head></head><body style="color:red;"><h1>header</h1></body></html>`
const htmlStr = rehype()
.use(rehypeRewrite, (node) => {
.use(rehypeRewrite, (node: any) => {
if (node.type == 'element' && node.tagName == 'body') {
node.properties = { ...node.properties, style: 'color:red;'}
}
@@ -47,7 +46,7 @@ describe('rehype-rewrite test case', () => {
const expected = `<h1>hello<span> world</span></h1>`
const htmlStr = rehype()
.data('settings', { fragment: true })
.use(rehypeRewrite, (node) => {
.use(rehypeRewrite, (node: any) => {
if (node.type == 'element' && node.tagName == 'h1') {
node.children = [ ...node.children, {
type: 'element',
@@ -73,7 +72,7 @@ describe('rehype-rewrite test case', () => {
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeRewrite, (node) => {
.use(rehypeRewrite, (node: any) => {
if (node.type == 'element' && node.tagName == 'p') {
node.properties = { ...node.properties, style: 'color:red;' }
}
@@ -90,7 +89,7 @@ describe('rehype-rewrite test case', () => {
const expected = `Hello World!`
const htmlStr = rehype()
.data('settings', { fragment: true })
.use(rehypeRewrite, {})
.use(rehypeRewrite, {} as any)
.use(stringify)
.processSync(markdown)
.toString()
33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"name": "rehype-rewrite",
"version": "1.0.2",
"version": "2.0.0",
"description": "Rewrite element with rehype.",
"author": "Kenny Wong <wowohoo@qq.com>",
"license": "MIT",
"sideEffects": false,
"type": "module",
"main": "lib/index.js",
"scripts": {
"prepack": "npm run build",
"start": "node lib/index.js",
"watch": "npm run watch:ts",
"watch:ts": "tsbb watch & npm run build:types -- --watch",
"build": "npm run build:ts && npm run build:types",
"build:ts": "tsbb build",
"build:types": "tsbb types",
"watch": "tsbb watch --no-esm --disable-babel",
"build": "tsbb build --no-esm --disable-babel",
"test": "tsbb test",
"coverage": "tsbb test --coverage"
},
@@ -37,20 +36,22 @@
"collectCoverageFrom": [
"lib/*.js",
"!lib/*.d.ts"
],
"transformIgnorePatterns": [
"<rootDir>/node_modules/?!(.*)"
]
},
"dependencies": {
"ts-mdast": "1.0.0",
"unified": "9.2.1"
"unified": "10.1.0"
},
"devDependencies": {
"@types/jest": "26.0.23",
"jest": "26.6.3",
"rehype": "11.0.0",
"rehype-stringify": "8.0.0",
"rehype-raw": "5.1.0",
"remark-parse": "9.0.0",
"remark-rehype": "8.1.0",
"tsbb": "2.1.2"
"@types/jest": "27.0.1",
"jest": "27.1.0",
"rehype": "12.0.0",
"rehype-stringify": "9.0.1",
"rehype-raw": "6.1.0",
"remark-parse": "10.0.0",
"remark-rehype": "9.0.0",
"tsbb": "3.0.3"
}
}
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Root, Parent, Content } from 'ts-mdast'
import { Plugin } from 'unified'
import visit from './visit'
import { Plugin, Transformer } from 'unified';
import { Parent, NodeData, Node } from 'unist';
import visit from './visit';

export type MdastTransformer = (tree: Root) => void
export type RehypeRewriteHandle = (node: Content, index: number, parent: Parent) => void
export type RehypeRewriteOptions = (node: NodeData<Parent>, index: number, parent: NodeData<Parent>) => void

const remarkRewrite: Plugin<[RehypeRewriteHandle?]> = (handle): MdastTransformer =>{
return (tree: Root) => {
visit(tree, (node: Content, index: number, parent: Parent) => {
const remarkRewrite: Plugin<[RehypeRewriteOptions?]> = (handle): Transformer => {
return transformer;
function transformer(tree: Node<NodeData<Parent>>): void {
visit(tree as any, (node: NodeData<Parent>, index: number, parent: NodeData<Parent>) => {
if (handle && typeof handle === 'function') {
handle(node, index, parent)
}
13 changes: 7 additions & 6 deletions src/visit.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Root, Parent, Content } from 'ts-mdast'
import { Parent, NodeData } from 'unist';

type Callback = (node: Content, index: number, parent: Parent | Content) => void

export default function visit(tree: Root, callback: Callback) {
export type VisitCallback = (node: NodeData<Parent>, index: number, parent: NodeData<Parent>) => void;
export default function visit(tree?: NodeData<Parent>, callback?: VisitCallback) {
if (tree && tree.children && Array.isArray(tree.children)) {
handle(tree.children, tree, callback)
}
}

function handle(tree: Content[], parent: Parent | Content, callback: Callback) {
function handle(tree: NodeData<Parent>[], parent: NodeData<Parent>, callback?: VisitCallback) {
tree.forEach((item, index) => {
callback(item, index, parent)
if (callback) {
callback(item, index, parent);
}
if (item && Array.isArray(item.children)) {
handle(item.children, item, callback)
}
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"sourceMap": true,
"declaration": true,
@@ -11,12 +11,12 @@
"strictNullChecks": true,
"strictPropertyInitialization": true,
"stripInternal": true,

"esModuleInterop": false,
"noImplicitAny": true,
"outDir": "lib",
"baseUrl": "."
},
"include": [
"src/**/*", ]
"src/**/*"
]
}