Skip to content

Commit 14c8605

Browse files
committed
commit ze plugin
Signed-off-by: rishichawda <[email protected]>
1 parent 6014e4d commit 14c8605

File tree

9 files changed

+2119
-0
lines changed

9 files changed

+2119
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
node_modules

README.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# remark-notes-plugin 📝
2+
3+
A powerful TypeScript remark plugin that transforms markdown blockquotes into beautifully styled note elements. Add professional-looking notes, tips, quotes, and more to your markdown documentation with minimal effort!
4+
5+
![npm](https://img.shields.io/npm/v/remark-notes-plugin)
6+
![License](https://img.shields.io/npm/l/remark-notes-plugin)
7+
8+
## ✨ Features
9+
10+
- 🎨 **5 Beautiful Note Types** - Note, Tip, Important, Quote, and Bonus
11+
- 🎯 **Semantic HTML Output** - Clean and accessible HTML structure
12+
- 💅 **Customizable Styling** - Easy to override CSS classes
13+
- 🔧 **Easy Integration** - Works with any remark-based markdown processor
14+
15+
## 📦 Installation
16+
17+
```bash
18+
npm install remark-notes-plugin
19+
```
20+
21+
## 🚀 Usage
22+
23+
```typescript
24+
import { unified } from 'unified'
25+
import remarkParse from 'remark-parse'
26+
import remarkStringify from 'remark-stringify'
27+
import remarkNotes from 'remark-notes-plugin'
28+
29+
const markdown = `
30+
> [!note]
31+
> This is a note about something important.
32+
33+
> [!tip]
34+
> Here's a helpful tip for you.
35+
36+
> [!important]
37+
> This is a very important message!
38+
39+
> [!quote]
40+
> Here's a memorable quote.
41+
42+
> [!bonus]
43+
> Here's some extra content for you!
44+
`
45+
46+
const file = await unified()
47+
.use(remarkParse)
48+
.use(remarkNotes)
49+
.use(remarkStringify)
50+
.process(markdown)
51+
52+
console.log(String(file))
53+
```
54+
55+
## 📝 Note Types
56+
57+
The plugin supports five distinct types of notes, each with its own unique style:
58+
59+
1. **Note** - For general information and remarks
60+
```markdown
61+
> [!note]
62+
> Your note content here
63+
```
64+
65+
2. **Tip** - For helpful tips and tricks
66+
```markdown
67+
> [!tip]
68+
> Your tip content here
69+
```
70+
71+
3. **Important** - For critical information and warnings
72+
```markdown
73+
> [!important]
74+
> Your important message here
75+
```
76+
77+
4. **Quote** - For quotations and references
78+
```markdown
79+
> [!quote]
80+
> Your quote content here
81+
```
82+
83+
5. **Bonus** - For additional, supplementary content
84+
```markdown
85+
> [!bonus]
86+
> Your bonus content here
87+
```
88+
89+
## 🎨 Styling
90+
91+
Include the provided `styles.css` in your project to get the default styling for the notes. The plugin uses a modular class structure that makes it easy to customize the appearance:
92+
93+
### Base Classes
94+
95+
- `.remark-note` - Base container for all note types
96+
- `.remark-note-header` - Note header container
97+
- `.remark-note-icon` - Icon styling
98+
- `.remark-note-title` - Note title styling
99+
- `.remark-note-content` - Note content container
100+
101+
### Type-Specific Classes
102+
103+
- `.remark-note.note` - Note type styling
104+
- `.remark-note.tip` - Tip type styling
105+
- `.remark-note.important` - Important type styling
106+
- `.remark-note.quote` - Quote type styling
107+
- `.remark-note.bonus` - Bonus type styling
108+
109+
### Customization Example
110+
111+
```css
112+
/* Example: Customize the Note type */
113+
.remark-note.note {
114+
background-color: #your-color;
115+
border-color: #your-border-color;
116+
}
117+
118+
.remark-note.note .remark-note-title {
119+
color: #your-text-color;
120+
}
121+
```
122+
123+
## 🛠️ Development
124+
125+
This project is written in TypeScript. To contribute or modify:
126+
127+
```bash
128+
# Install dependencies
129+
yarn
130+
131+
# Build the project
132+
yarn build
133+
134+
# Run tests
135+
yarn test
136+
137+
# Watch mode for development
138+
yarn watch
139+
```
140+
141+
## 🤝 Contributing
142+
143+
Contributions are welcome! Feel free to:
144+
145+
- Report bugs
146+
- Suggest new features
147+
- Submit pull requests
148+
149+
Please ensure your pull request passes all tests and includes appropriate documentation.
150+
151+
---
152+
153+
⭐️ If you find this plugin useful, please consider giving it a star on GitHub! ⭐️

index.ts

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import { visit } from 'unist-util-visit'
2+
import type { Node } from 'unist'
3+
import type { Blockquote, Paragraph, Text, BlockContent, DefinitionContent } from 'mdast'
4+
import { readFileSync } from 'fs'
5+
import { fileURLToPath } from 'url'
6+
import { dirname, join } from 'path'
7+
import { toHtml } from 'hast-util-to-html'
8+
import { toHast } from 'mdast-util-to-hast'
9+
10+
interface NoteType {
11+
icon: string
12+
type: string
13+
}
14+
15+
interface NoteTypes {
16+
[key: string]: NoteType
17+
}
18+
19+
const noteTypes: NoteTypes = {
20+
'note': {
21+
icon: `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
22+
<g stroke-linecap="round" stroke-linejoin="round" stroke-width="0.048">
23+
</g>
24+
<g>
25+
<path d="M8 2V5" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round">
26+
</path>
27+
<path d="M16 2V5" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round">
28+
</path>
29+
<path d="M21 8.5V17C21 20 19.5 22 16 22H8C4.5 22 3 20 3 17V8.5C3 5.5 4.5 3.5 8 3.5H16C19.5 3.5 21 5.5 21 8.5Z"
30+
stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round">
31+
</path>
32+
<path d="M8 11H16" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round">
33+
</path>
34+
<path d="M8 16H12" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round">
35+
</path>
36+
</g>
37+
</svg>`,
38+
type: 'note'
39+
},
40+
'tip': {
41+
icon: `<svg viewBox="-0.5 0 25 25" fill="none" xmlns="http://www.w3.org/2000/svg">
42+
<g stroke-linecap="round" stroke-linejoin="round"></g>
43+
<g>
44+
<path
45+
d="M19.0006 9.03002C19.0007 8.10058 18.8158 7.18037 18.4565 6.32317C18.0972 5.46598 17.5709 4.68895 16.9081 4.03734C16.2453 3.38574 15.4594 2.87265 14.5962 2.52801C13.7331 2.18336 12.8099 2.01409 11.8806 2.03002C10.0966 2.08307 8.39798 2.80604 7.12302 4.05504C5.84807 5.30405 5.0903 6.98746 5.00059 8.77001C4.95795 9.9595 5.21931 11.1402 5.75999 12.2006C6.30067 13.2609 7.10281 14.1659 8.09058 14.83C8.36897 15.011 8.59791 15.2584 8.75678 15.5499C8.91565 15.8415 8.99945 16.168 9.00059 16.5V18.03H15.0006V16.5C15.0006 16.1689 15.0829 15.843 15.24 15.5515C15.3971 15.26 15.6241 15.0121 15.9006 14.83C16.8528 14.1911 17.6336 13.328 18.1741 12.3167C18.7147 11.3054 18.9985 10.1767 19.0006 9.03002Z"
46+
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
47+
<path d="M15 21.04C14.1345 21.6891 13.0819 22.04 12 22.04C10.9181 22.04 9.86548 21.6891 9 21.04"
48+
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
49+
<path
50+
d="M11.9901 5.64001L10.3301 8.41998C10.2549 8.54184 10.2138 8.68167 10.2111 8.82483C10.2084 8.96799 10.2441 9.10925 10.3146 9.23389C10.3851 9.35852 10.4877 9.46195 10.6118 9.53339C10.7359 9.60482 10.8769 9.64165 11.0201 9.64001H13.0201C13.1617 9.63947 13.301 9.67657 13.4237 9.7475C13.5463 9.81843 13.6479 9.92063 13.7181 10.0437C13.7883 10.1668 13.8245 10.3063 13.8231 10.4479C13.8217 10.5896 13.7827 10.7283 13.7101 10.85L12.0301 13.64"
51+
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
52+
</g>
53+
</svg>`,
54+
type: 'tip'
55+
},
56+
'important': {
57+
icon: `<svg viewBox="0 0 18 18" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
58+
<g stroke-width="0"></g>
59+
<g stroke-linecap="round" stroke-linejoin="round"></g>
60+
<g>
61+
<path fill="currentColor" d="M9,14a1.5,1.5,0,1,1,1.5068-1.5A1.5035,1.5035,0,0,1,9,14Z"></path>
62+
<path fill="currentColor" d="M9,2A7,7,0,1,1,2,9,7.0079,7.0079,0,0,1,9,2M9,0a9,9,0,1,0,9,9A9,9,0,0,0,9,0Z"></path>
63+
<path fill="currentColor" d="M10,4H8a1,1,0,0,0-.97,1.2425l1,4a1,1,0,0,0,1.94,0l1-4A1,1,0,0,0,10,4Zm0,2h0Z"></path>
64+
</g>
65+
</svg>`,
66+
type: 'important'
67+
},
68+
'quote': {
69+
icon: `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
70+
<g stroke-linecap="round" stroke-linejoin="round"></g>
71+
<g>
72+
<path
73+
d="M14 15V14C14 13.0681 14 12.6022 14.1522 12.2346C14.3552 11.7446 14.7446 11.3552 15.2346 11.1522C15.6022 11 16.0681 11 17 11H17.5C18.9045 11 19.6067 11 20.1111 11.3371C20.3295 11.483 20.517 11.6705 20.6629 11.8889C21 12.3933 21 13.0955 21 14.5V15.3431C21 16.1606 21 16.5694 20.8478 16.9369C20.6955 17.3045 20.4065 17.5935 19.8284 18.1716L19.2396 18.7604C18.7822 19.2178 18 18.8938 18 18.2469V17.8787C18 17.3934 17.6066 17 17.1213 17H16C14.8954 17 14 16.1046 14 15Z"
74+
stroke-width="1.5" stroke-linejoin="round"></path>
75+
<path
76+
d="M3 9V8C3 7.06812 3 6.60218 3.15224 6.23463C3.35523 5.74458 3.74458 5.35523 4.23463 5.15224C4.60218 5 5.06812 5 6 5H6.5C7.90446 5 8.60669 5 9.11114 5.33706C9.32952 5.48298 9.51702 5.67048 9.66294 5.88886C10 6.39331 10 7.09554 10 8.5V9.34315C10 10.1606 10 10.5694 9.84776 10.9369C9.69552 11.3045 9.40649 11.5935 8.82843 12.1716L8.23965 12.7604C7.78219 13.2178 7 12.8938 7 12.2469V11.8787C7 11.3934 6.6066 11 6.12132 11H5C3.89543 11 3 10.1046 3 9Z"
77+
stroke-width="1.5" stroke-linejoin="round"></path>
78+
</g>
79+
</svg>`,
80+
type: 'quote'
81+
},
82+
'bonus': {
83+
icon: `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
84+
<g stroke-linecap="round" stroke-linejoin="round"></g>
85+
<g>
86+
<path
87+
d="M9.23163 8.61762C7.26389 9.06284 6.28001 9.28545 6.04594 10.0382C5.81186 10.7909 6.4826 11.5753 7.82408 13.1439L8.17113 13.5498C8.55234 13.9955 8.74294 14.2184 8.82869 14.4942C8.91444 14.7699 8.88562 15.0673 8.82799 15.662L8.77552 16.2035C8.5727 18.2965 8.4713 19.343 9.08412 19.8082C9.69694 20.2734 10.6181 19.8492 12.4605 19.0009L12.9372 18.7815C13.4607 18.5404 13.7225 18.4199 14 18.4199C14.2775 18.4199 14.5393 18.5404 15.0628 18.7815L15.5395 19.0009C17.3819 19.8492 18.3031 20.2734 18.9159 19.8082C19.5287 19.343 19.4273 18.2965 19.2245 16.2035M20.1759 13.1439C21.5174 11.5753 22.1881 10.7909 21.9541 10.0382C21.72 9.28545 20.7361 9.06284 18.7684 8.61762L18.2593 8.50244C17.7001 8.37592 17.4205 8.31266 17.196 8.14225C16.9716 7.97183 16.8276 7.71355 16.5396 7.19699L16.2775 6.7267C15.2641 4.9089 14.7575 4 14 4C13.2425 4 12.7359 4.9089 11.7225 6.7267"
88+
stroke-width="1.5" stroke-linecap="round"></path>
89+
<path d="M2.08887 16C3.20445 15.121 4.68639 14.7971 6.08887 15.1257" stroke-width="1.5"
90+
stroke-linecap="round"></path>
91+
<path d="M2.08887 10.5C3.08887 10 3.37862 10.0605 4.08887 10" stroke-width="1.5"
92+
stroke-linecap="round"></path>
93+
<path d="M2 5.60867L2.20816 5.48676C4.41383 4.19506 6.75032 3.84687 8.95304 4.48161L9.16092 4.54152"
94+
stroke-width="1.5" stroke-linecap="round"></path>
95+
</g>
96+
</svg>`,
97+
type: 'bonus'
98+
}
99+
}
100+
101+
// Get the styles content
102+
const __filename = fileURLToPath(import.meta.url)
103+
const __dirname = dirname(__filename)
104+
// Look for styles.css in the package root (one level up from dist)
105+
const stylesPath = join(__dirname, 'styles.css')
106+
const styles = readFileSync(stylesPath, 'utf-8')
107+
108+
export default function remarkNotes() {
109+
return (tree: Node) => {
110+
let hasInjectedStyles = false;
111+
112+
visit(tree, 'root', (node: any) => {
113+
if (!hasInjectedStyles) {
114+
// Inject styles at the beginning of the document
115+
node.children.unshift({
116+
type: 'html',
117+
value: `<style>${styles}</style>`
118+
});
119+
hasInjectedStyles = true;
120+
}
121+
});
122+
123+
visit(tree, 'blockquote', (node: Blockquote) => {
124+
const firstParagraph = node.children[0] as Paragraph
125+
if (!firstParagraph) return
126+
127+
const firstChild = firstParagraph.children[0]
128+
if (!firstChild || firstChild.type !== 'text') return
129+
130+
const textNode = firstChild as Text
131+
if (!textNode) return
132+
133+
// Updated pattern to match [!type]
134+
const match = textNode.value.match(/^\[!(\w+)\]/)
135+
if (!match) return
136+
137+
const noteType = match[1].toLowerCase() // Convert to lowercase to match our types
138+
139+
if (noteType in noteTypes) {
140+
const noteConfig = noteTypes[noteType]
141+
142+
// Create a clone of the blockquote children to preserve markdown structure
143+
const children = [...node.children]
144+
145+
// Process the first paragraph to remove the note marker
146+
if (children.length > 0 && children[0].type === 'paragraph') {
147+
const firstPara = children[0] as Paragraph
148+
const firstTextNode = firstPara.children[0] as Text
149+
150+
if (firstTextNode && firstTextNode.type === 'text') {
151+
// Only remove the marker from the text content
152+
firstTextNode.value = firstTextNode.value.replace(/^\[!\w+\]\s*/, '')
153+
154+
// If the text node is now empty and it's the only child, remove it
155+
if (firstTextNode.value === '' && firstPara.children.length === 1) {
156+
children.shift()
157+
}
158+
}
159+
}
160+
161+
// Convert the modified markdown structure to HTML
162+
const contentHast = toHast({
163+
type: 'root',
164+
children: children
165+
}) as any
166+
167+
// Convert hast to HTML string
168+
const contentHtml = contentHast ? toHtml(contentHast) : ''
169+
170+
// Create HTML structure using classes
171+
const html = {
172+
type: 'html',
173+
value: `
174+
<div class="remark-note ${noteConfig.type}">
175+
<div class="remark-note-header">
176+
<span class="remark-note-icon" data-type="${noteConfig.type}">${noteConfig.icon}</span>
177+
<span class="remark-note-title">${noteType}</span>
178+
</div>
179+
<div class="remark-note-content">
180+
${contentHtml}
181+
</div>
182+
</div>
183+
`
184+
}
185+
186+
// Replace the blockquote with our HTML
187+
Object.assign(node, html)
188+
}
189+
})
190+
}
191+
}

0 commit comments

Comments
 (0)