Skip to content

Commit a4a35cd

Browse files
authored
feat: add new unplugins package (#9)
1 parent 2829682 commit a4a35cd

20 files changed

+393
-155
lines changed

eslint.config.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
jsConfig,
3-
tsConfig,
4-
svelteConfig,
5-
ignoresConfig,
6-
} from '@sveltek/eslint-config'
1+
import { jsConfig, tsConfig, ignoresConfig } from '@sveltek/eslint-config'
72

83
/** @type {import("eslint").Linter.Config[]} */
9-
export default [jsConfig, tsConfig, svelteConfig, ignoresConfig]
4+
export default [jsConfig, tsConfig, ignoresConfig]

package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@
55
"private": true,
66
"type": "module",
77
"scripts": {
8-
"build:md": "pnpm --filter @sveltek/markdown build",
9-
"publish:md": "pnpm --filter @sveltek/markdown publish:md",
10-
"lint:md": "pnpm --filter @sveltek/markdown lint",
11-
"format:md": "pnpm --filter @sveltek/markdown format",
8+
"build:markdown": "pnpm --filter @sveltek/markdown build",
9+
"lint:markdown": "pnpm --filter @sveltek/markdown lint",
10+
"format:markdown": "pnpm --filter @sveltek/markdown format",
11+
"publish:markdown": "pnpm --filter @sveltek/markdown publish:markdown",
12+
"build:unplugins": "pnpm --filter @sveltek/unplugins build",
13+
"lint:unplugins": "pnpm --filter @sveltek/unplugins lint",
14+
"format:unplugins": "pnpm --filter @sveltek/unplugins format",
15+
"publish:unplugins": "pnpm --filter @sveltek/unplugins publish:unplugins",
1216
"dev:play": "pnpm --filter playground-sveltekit dev",
1317
"build:play": "pnpm --filter playground-sveltekit build",
1418
"preview:play": "pnpm --filter playground-sveltekit preview",
1519
"sync:play": "pnpm --filter playground-sveltekit sync",
1620
"lint:play": "pnpm --filter playground-sveltekit lint",
1721
"format:play": "pnpm --filter playground-sveltekit format",
1822
"format:root": "prettier --write . '!playgrounds' '!packages'",
23+
"create:release": "gh workflow run release --inputs package_name=$1",
1924
"update:all": "taze -r --interactive"
2025
},
2126
"packageManager": "[email protected]",

packages/markdown/eslint.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { jsConfig, tsConfig, ignoresConfig } from '@sveltek/eslint-config'
2+
3+
/** @type {import("eslint").Linter.Config[]} */
4+
export default [jsConfig, tsConfig, ignoresConfig]

packages/markdown/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"lint": "eslint .",
4646
"lint:fix": "eslint --fix .",
4747
"format": "prettier --write .",
48-
"publish:md": "npm run build && npm publish"
48+
"publish:markdown": "npm run build && npm publish"
4949
},
5050
"peerDependencies": {
5151
"@types/node": ">=20.0.0",

packages/unplugins/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
node_modules
3+
*.log*
4+
.private
5+
.cache
6+
.env
7+
.npmrc
8+
dist

packages/unplugins/.prettierignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
node_modules
3+
*-lock.*
4+
*.log*
5+
.private
6+
.cache
7+
.env
8+
dist

packages/unplugins/LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Ivo Dolenc <https://github.com/ivodolenc>
4+
Copyright (c) 2025 Sveltek <https://github.com/sveltek>
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

packages/unplugins/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<h1 align="center">@sveltek/unplugins</h1>
2+
3+
<p align="center">Sveltek's Unified plugins for Markdown preprocessor.</p>
4+
5+
<br>
6+
7+
## Community
8+
9+
Feel free to ask questions or share new ideas.
10+
11+
Use the official [discussions](https://github.com/sveltek/markdown/discussions) to get involved.
12+
13+
## Contribute
14+
15+
Check out the quick [guide](https://github.com/sveltek/markdown/blob/main/.github/CONTRIBUTING.md) for more info.
16+
17+
## License
18+
19+
Developed in 🇭🇷 Croatia, © Sveltek.
20+
21+
Released under the [MIT](LICENSE.txt) license.

packages/unplugins/bundler.config.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from '@hypernym/bundler'
2+
3+
export default defineConfig({
4+
entries: [
5+
// Main
6+
{
7+
input: './src/index.ts',
8+
},
9+
{
10+
dts: './src/types.ts',
11+
output: './dist/index.d.mts',
12+
},
13+
],
14+
})

packages/unplugins/eslint.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { jsConfig, tsConfig, ignoresConfig } from '@sveltek/eslint-config'
2+
3+
/** @type {import("eslint").Linter.Config[]} */
4+
export default [jsConfig, tsConfig, ignoresConfig]

packages/unplugins/package.json

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "@sveltek/unplugins",
3+
"version": "0.10.0",
4+
"author": "Sveltek",
5+
"description": "Sveltek's Unified plugins for Markdown preprocessor.",
6+
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/sveltek/markdown",
10+
"directory": "packages/unplugins"
11+
},
12+
"homepage": "https://github.com/sveltek/markdown",
13+
"type": "module",
14+
"exports": {
15+
".": {
16+
"types": "./dist/index.d.mts",
17+
"import": "./dist/index.mjs"
18+
}
19+
},
20+
"files": [
21+
"dist"
22+
],
23+
"keywords": [
24+
"sveltek",
25+
"svelte",
26+
"sveltekit",
27+
"svelte-markdown",
28+
"markdown",
29+
"plugins",
30+
"unified",
31+
"rehype",
32+
"remark"
33+
],
34+
"scripts": {
35+
"build": "hyperbundler",
36+
"lint": "eslint .",
37+
"lint:fix": "eslint --fix .",
38+
"format": "prettier --write .",
39+
"publish:unplugins": "npm run build && npm publish"
40+
},
41+
"peerDependencies": {
42+
"@types/node": ">=20.0.0",
43+
"typescript": ">=5.0.0"
44+
},
45+
"peerDependenciesMeta": {
46+
"@types/node": {
47+
"optional": true
48+
},
49+
"typescript": {
50+
"optional": true
51+
}
52+
},
53+
"dependencies": {
54+
"@types/hast": "^3.0.4",
55+
"@types/mdast": "^4.0.4",
56+
"unified": "^11.0.5",
57+
"unist-util-visit": "^5.0.0"
58+
}
59+
}

packages/unplugins/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './remark'
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './toc'
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { visit } from 'unist-util-visit'
2+
import type { Properties } from 'hast'
3+
import type { Root } from 'mdast'
4+
import type { Plugin } from '@/types'
5+
import type { TocOptions, TocItems } from './types'
6+
7+
/**
8+
* A custom `Remark` plugin that creates `Table of Content` (Toc).
9+
*
10+
* Automatically adds a link with the appropriate attributes to the headings.
11+
*
12+
* It also stores Toc items to `frontmatter` for easy access.
13+
*
14+
* @example
15+
*
16+
* ```ts
17+
* import { svelteMarkdown } from '@sveltek/markdown'
18+
* import { remarkToc } from '@sveltek/unplugins'
19+
*
20+
* svelteMarkdown({
21+
* plugins: {
22+
* remark: [remarkToc]
23+
* }
24+
* })
25+
* ```
26+
*
27+
* Or with options:
28+
*
29+
* ```js
30+
* svelteMarkdown({
31+
* plugins: {
32+
* remark: [[remarkToc, { depth: 3 }]]
33+
* }
34+
* })
35+
* ```
36+
*/
37+
export const remarkToc: Plugin<[TocOptions?], Root> = (
38+
options: TocOptions = {},
39+
) => {
40+
const { depth = 3, links = true } = options
41+
42+
return (tree, vfile) => {
43+
const frontmatter = vfile.data.frontmatter as {
44+
toc: TocItems
45+
}
46+
47+
const toc: TocItems = []
48+
let i = 0
49+
50+
visit(tree, 'heading', (node) => {
51+
const [child] = node.children
52+
53+
let value = ''
54+
let id = ''
55+
56+
if (child.type === 'text') {
57+
value = child.value
58+
id = value
59+
.toLowerCase()
60+
.replace(/[^a-z0-9\s-]/g, '')
61+
.trim()
62+
.replace(/\s+/g, '-')
63+
}
64+
65+
if (links) {
66+
node.children = []
67+
node.children.push({
68+
type: 'link',
69+
url: `#${id}`,
70+
children: [{ type: 'text', value }],
71+
})
72+
}
73+
74+
const data = (node.data || (node.data = {})) as {
75+
hProperties?: Properties
76+
}
77+
const props = data.hProperties || (data.hProperties = {})
78+
79+
if (node.depth > 1 && node.depth <= depth) {
80+
if (toc.some((h) => h.id === id)) id = `${id}-${i + 1}`
81+
if (!props.id) props.id = id
82+
toc.push({ id, depth: node.depth, value })
83+
}
84+
})
85+
86+
frontmatter.toc = toc
87+
}
88+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export interface TocOptions {
2+
/**
3+
* Specifies the maximum headings depth to be included in the table of content.
4+
*
5+
* @default 3
6+
*/
7+
depth?: number
8+
/**
9+
* Specifies whether headings include link tags.
10+
*
11+
* @default true
12+
*/
13+
links?: boolean
14+
}
15+
16+
export interface TocItem {
17+
id: string
18+
depth: number
19+
value: string
20+
}
21+
22+
export type TocItems = TocItem[]
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './toc/types'
2+
3+
export * from './'

packages/unplugins/src/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type * as Unified from 'unified'
2+
export type { Plugin } from 'unified'
3+
export type * as Mdast from 'mdast'
4+
export type * as Hast from 'hast'
5+
6+
export * from './remark/types'

packages/unplugins/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@sveltek/tsconfig"
3+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {
2+
jsConfig,
3+
tsConfig,
4+
svelteConfig,
5+
ignoresConfig,
6+
} from '@sveltek/eslint-config'
7+
8+
/** @type {import("eslint").Linter.Config[]} */
9+
export default [jsConfig, tsConfig, svelteConfig, ignoresConfig]

0 commit comments

Comments
 (0)