Skip to content

Commit fccf0e5

Browse files
JayaKrishnaNamburualexnm
authored andcommitted
feat(rehype-html): adding rehype as a html processor (#369)
* feat(rehype-html): adding rehype as a html processor * refactor(rehype-html): configured rehype-parser to use in gragment mode
1 parent 2a97b71 commit fccf0e5

File tree

10 files changed

+1636
-12
lines changed

10 files changed

+1636
-12
lines changed

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020
"testPathIgnorePatterns": ['mocks.ts'],
2121
"moduleNameMapper": {
2222
'^html-whitespace-sensitive-tag-names$':
23-
'<rootDir>/packages/teleport-component-generator-vue/setup/html-whitespace-sensitive-tag-names.json'
23+
'<rootDir>/test-mocks/html-whitespace-sensitive-tag-names.json'
2424
},
2525
"collectCoverage": false,
2626
"testEnvironment": "node",

packages/teleport-component-generator-vue/setup/html-whitespace-sensitive-tag-names.json

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# teleport-postprocessor-rehype-html
2+
3+
A post-processing function that formats html code chunks using rehype and the rehype-format plugin.
4+
5+
> This package is part of the [teleport ecosystem](https://github.com/teleporthq/teleport-code-generators). For a complete guide, check out the [official documentation](https://docs.teleporthq.io/).
6+
7+
## Install
8+
```bash
9+
npm install @teleporthq/teleport-postprocessor-rehype-html
10+
```
11+
or
12+
```bash
13+
yarn add @teleporthq/teleport-postprocessor-rehype-html
14+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import processor from '../src'
2+
3+
describe('rehype html', () => {
4+
it('formats only the html chunk', () => {
5+
const inputChunks = {
6+
html: `<div><span >Format me!</span>
7+
</div>`,
8+
js: `import React from "react"; import {Link} from "react-router"`,
9+
}
10+
11+
const result = processor(inputChunks)
12+
13+
expect(result.html).toBe(`\n<div><span>Format me!</span></div>\n`)
14+
expect(result.js).toBe(inputChunks.js)
15+
})
16+
17+
it('skips formatting if no html chunk is found', () => {
18+
const inputChunks = {
19+
css: '.test { margin: 10px; }',
20+
js: `import React from "react"; import {Link} from "react-router"`,
21+
}
22+
23+
const result = processor(inputChunks)
24+
25+
expect(result.css).toBe(inputChunks.css)
26+
expect(result.js).toBe(inputChunks.js)
27+
})
28+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@teleporthq/teleport-postprocessor-rehype-html",
3+
"version": "0.10.0-alpha.0",
4+
"description": "A post-processing function that formats html code chunks using rehype and the rehype-html plugin",
5+
"author": "teleportHQ",
6+
"license": "MIT",
7+
"homepage": "https://teleporthq.io/",
8+
"main": "dist/cjs/index.js",
9+
"types": "dist/cjs/index.d.ts",
10+
"module": "dist/esm/index.js",
11+
"repository": {
12+
"type": "git",
13+
"url": "git+ssh://[email protected]/teleporthq/teleport-code-generators.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/teleporthq/teleport-code-generators/issues"
17+
},
18+
"publishConfig": {
19+
"access": "public"
20+
},
21+
"scripts": {
22+
"clean": "rimraf dist",
23+
"build": "npm run build:cjs & npm run build:esm",
24+
"build:cjs": "tsc -p tsconfig-cjs.json",
25+
"build:esm": "tsc -p tsconfig-esm.json"
26+
},
27+
"dependencies": {
28+
"rehype": "^9.0.0",
29+
"rehype-format": "^3.0.0",
30+
"rehype-parse": "^6.0.1",
31+
"@teleporthq/teleport-types": "^0.10.0-alpha.0"
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// @ts-ignore
2+
import rehype from 'rehype'
3+
// @ts-ignore
4+
import format from 'rehype-format'
5+
// @ts-ignore
6+
import parse from 'rehype-parse'
7+
8+
import { PostProcessor, PrettierFormatOptions, FileType } from '@teleporthq/teleport-types'
9+
10+
interface PostProcessorFactoryOptions {
11+
fileType?: string
12+
formatOptions?: PrettierFormatOptions
13+
}
14+
15+
export const createPostProcessor = (options: PostProcessorFactoryOptions = {}) => {
16+
const fileType = options.fileType || FileType.HTML
17+
18+
const processor: PostProcessor = (codeChunks) => {
19+
if (codeChunks[fileType]) {
20+
rehype()
21+
.use(parse, { fragment: true })
22+
.use(format)
23+
.process(codeChunks[fileType], (err: any, file: any) => {
24+
codeChunks[fileType] = String(file)
25+
if (err) {
26+
throw new Error(`Error occured in formatting with rehype, ${err}`)
27+
}
28+
})
29+
} else {
30+
console.warn('No code chunk of type HTML found, prettier-html did not perform any operation')
31+
}
32+
33+
return codeChunks
34+
}
35+
36+
return processor
37+
}
38+
39+
export default createPostProcessor()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist/cjs",
5+
"module": "commonjs",
6+
"target": "es5"
7+
},
8+
"include": [
9+
"./src"
10+
]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist/esm",
5+
"module": "esnext",
6+
"target": "esnext",
7+
"moduleResolution": "node"
8+
},
9+
"include": [
10+
"./src"
11+
]
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
"script",
3+
"style",
4+
"pre",
5+
"textarea"
6+
]

0 commit comments

Comments
 (0)