-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce transformers package (#32)
- Loading branch information
Showing
41 changed files
with
786 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,5 +36,6 @@ | |
"json", | ||
"jsonc", | ||
"yaml" | ||
] | ||
], | ||
"references.preferredLocation": "peek" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# shikiji-transformers | ||
|
||
Collective of common transformers for [shikiji](https://github.com/antfu/shikiji), inspired by [shiki-processor](https://github.com/innocenzi/shiki-processor). | ||
|
||
## Install | ||
|
||
```bash | ||
npm i -D shikiji-transformers | ||
``` | ||
|
||
```ts | ||
import { | ||
codeToHtml, | ||
} from 'shikiji' | ||
import { | ||
transformerNotationDiff, | ||
// ... | ||
} from 'shikiji-transformers' | ||
|
||
const html = codeToHtml(code, { | ||
lang: 'ts', | ||
transformers: [ | ||
transformerNotationDiff(), | ||
// ... | ||
], | ||
}) | ||
``` | ||
|
||
## Transformers | ||
|
||
### `transformerNotationDiff` | ||
|
||
Use `[!code ++]` and `[!code --]` to mark added and removed lines. | ||
|
||
For example, the following code | ||
|
||
```ts | ||
export function foo() { | ||
console.log('hewwo') // [!code --] | ||
console.log('hello') // [!code ++] | ||
} | ||
``` | ||
|
||
will be transformed to | ||
|
||
```html | ||
<!-- Output (stripped of `style` attributes for clarity) --> | ||
<pre class="shiki has-diff"> <!-- Notice `has-diff` --> | ||
<code> | ||
<span class="line"></span> | ||
<span class="line"><span>function</span><span>()</span><span></span><span>{</span></span> | ||
<span class="line diff remove"> <!-- Notice `diff` and `remove` --> | ||
<span></span><span>console</span><span>.</span><span>log</span><span>(</span><span>'</span><span>hewwo</span><span>'</span><span>) </span> | ||
</span> | ||
<span class="line diff add"> <!-- Notice `diff` and `add` --> | ||
<span></span><span>console</span><span>.</span><span>log</span><span>(</span><span>'</span><span>hello</span><span>'</span><span>) </span> | ||
</span> | ||
<span class="line"><span></span><span>}</span></span> | ||
<span class="line"><span></span></span> | ||
</code> | ||
</pre> | ||
``` | ||
|
||
With some CSS, you can make it look like this: | ||
|
||
<img width="362" alt="image" src="https://github.com/antfu/shikiji/assets/11247099/c1b6d2b2-686c-4d36-87b1-f01877071cde"> | ||
|
||
### `transformerNotationHighlight` | ||
|
||
Use `[!code highlight]` to highlight a line (adding `highlighted` class). | ||
|
||
### `transformerNotationFocus` | ||
|
||
Use `[!code focus]` to focus a line (adding `focused` class). | ||
|
||
### `transformerNotationErrorLevel` | ||
|
||
Use `[!code error]`, `[!code warning]`, to mark a line with an error level (adding `highlighted error`, `highlighted warning` class). | ||
|
||
### `transformerRenderWhitespace` | ||
|
||
Render whitespaces (tabs and spaces) as individual spans, with classes `tab` and `space`. | ||
|
||
With some CSS, you can make it look like this: | ||
|
||
<img width="293" alt="image" src="https://github.com/antfu/shikiji/assets/11247099/01b7c4ba-6d63-4e74-8fd7-68a9f901f3de"> | ||
|
||
### `transformerCompactLineOptions` | ||
|
||
Support for `shiki`'s `lineOptions` that is removed in `shikiji`. | ||
|
||
### `transformerRemoveLineBreak` | ||
|
||
Remove line breaks between `<span class="line">`. Useful when you set `display: block` to `.line` in CSS. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index.ts', | ||
], | ||
declaration: true, | ||
rollup: { | ||
emitCJS: false, | ||
}, | ||
externals: [ | ||
'hast', | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "shikiji-transformers", | ||
"type": "module", | ||
"version": "0.0.0", | ||
"description": "Collective of common transformers transformers for Shikiji", | ||
"author": "Anthony Fu <anthonyfu117@hotmail.com>", | ||
"license": "MIT", | ||
"homepage": "https://github.com/antfu/shikiji#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/antfu/shikiji.git", | ||
"directory": "packages/shikiji-transformers" | ||
}, | ||
"bugs": "https://github.com/antfu/shikiji/issues", | ||
"keywords": [ | ||
"shiki", | ||
"shikiji-transformers" | ||
], | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.mts", | ||
"default": "./dist/index.mjs" | ||
} | ||
}, | ||
"main": "./dist/index.mjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.mts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "unbuild", | ||
"dev": "unbuild --stub", | ||
"prepublishOnly": "nr build" | ||
}, | ||
"dependencies": { | ||
"shikiji": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export * from './transformers/render-whitespace' | ||
export * from './transformers/remove-line-breaks' | ||
export * from './transformers/compact-line-options' | ||
export * from './transformers/notation-focus' | ||
export * from './transformers/notation-highlight' | ||
export * from './transformers/notation-diff' | ||
export * from './transformers/notation-error-level' | ||
export * from './utils' |
27 changes: 27 additions & 0 deletions
27
packages/shikiji-transformers/src/transformers/compact-line-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { ShikijiTransformer } from 'shikiji' | ||
import { addClassToHast } from 'shikiji' | ||
|
||
export interface TransformerCompactLineOption { | ||
/** | ||
* 1-based line number. | ||
*/ | ||
line: number | ||
classes?: string[] | ||
} | ||
|
||
/** | ||
* Transformer for `shiki`'s legacy `lineOptions` | ||
*/ | ||
export function transformerCompactLineOptions( | ||
lineOptions: TransformerCompactLineOption[] = [], | ||
): ShikijiTransformer { | ||
return { | ||
name: 'shikiji-transformers:compact-line-options', | ||
line(node, line) { | ||
const lineOption = lineOptions.find(o => o.line === line) | ||
if (lineOption?.classes) | ||
addClassToHast(node, lineOption.classes) | ||
return node | ||
}, | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/shikiji-transformers/src/transformers/notation-diff.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { ShikijiTransformer } from 'shikiji' | ||
import { addClassToHast } from 'shikiji' | ||
import { createCommentNotationTransformer } from '../utils' | ||
|
||
export interface TransformerNotationDiffOptions { | ||
/** | ||
* Class for added lines | ||
*/ | ||
classAdded?: string | ||
/** | ||
* Class for removed lines | ||
*/ | ||
classRemoved?: string | ||
/** | ||
* Class added to the root element when the current code has diff | ||
*/ | ||
classRootActive?: string | ||
} | ||
|
||
/** | ||
* Use `[!code ++]` and `[!code --]` to mark added and removed lines. | ||
*/ | ||
export function transformerNotationDiff( | ||
options: TransformerNotationDiffOptions = {}, | ||
): ShikijiTransformer { | ||
const { | ||
classAdded = 'diff add', | ||
classRemoved = 'diff remove', | ||
classRootActive = 'has-diff', | ||
} = options | ||
|
||
return createCommentNotationTransformer( | ||
'shikiji-transformers:notation-diff', | ||
/\[!code (\-\-|\+\+)\]/, | ||
function ([_, match], line) { | ||
const className = match === '--' | ||
? classRemoved | ||
: classAdded | ||
addClassToHast(line, className) | ||
if (classRootActive) | ||
addClassToHast(this.pre, classRootActive) | ||
return true | ||
}, | ||
) | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/shikiji-transformers/src/transformers/notation-error-level.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { ShikijiTransformer } from 'shikiji' | ||
import { addClassToHast } from 'shikiji' | ||
import { createCommentNotationTransformer } from '../utils' | ||
|
||
export interface TransformerNotationErrorLevelOptions { | ||
classMap?: Record<string, string | string[]> | ||
} | ||
|
||
/** | ||
* Allow using `[!code error]` `[!code warning]` notation in code to mark highlighted lines. | ||
*/ | ||
export function transformerNotationErrorLevel( | ||
options: TransformerNotationErrorLevelOptions = {}, | ||
): ShikijiTransformer { | ||
const { | ||
classMap = { | ||
error: ['highlighted', 'error'], | ||
warning: ['highlighted', 'warning'], | ||
}, | ||
} = options | ||
|
||
return createCommentNotationTransformer( | ||
'shikiji-transformers:notation-error-level', | ||
new RegExp(`\\[!code (${Object.keys(classMap).join('|')})\\]`), | ||
([_, match], line) => { | ||
addClassToHast(line, classMap[match]) | ||
return true | ||
}, | ||
) | ||
} |
Oops, something went wrong.