Skip to content

Commit 6fc0922

Browse files
authored
Merge pull request #3 from rishichawda/improvements
major improvements: - a lot of refactoring: there was a lot of html templating going on. but I found out that I need to properly implement markdown AST nodes. - added a bunch of tests using fixtures. - added proper configuration options to have more control over styles
2 parents b4636da + caedb8e commit 6fc0922

31 files changed

+1975
-336
lines changed

.github/workflows/test.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
paths:
7+
- 'lib/**'
8+
- 'index.ts'
9+
- 'styles.css'
10+
- 'package.json'
11+
- '__tests__/**'
12+
- 'tsconfig.json'
13+
- '.github/workflows/test.yml'
14+
push:
15+
branches: [main]
16+
paths:
17+
- 'lib/**'
18+
- 'index.ts'
19+
- 'styles.css'
20+
- 'package.json'
21+
- '__tests__/**'
22+
- 'tsconfig.json'
23+
- '.github/workflows/test.yml'
24+
25+
jobs:
26+
test:
27+
runs-on: ubuntu-latest
28+
strategy:
29+
matrix:
30+
node-version: [18, 20, 22]
31+
32+
steps:
33+
- name: Checkout
34+
uses: actions/checkout@v4
35+
36+
- name: Setup Node.js ${{ matrix.node-version }}
37+
uses: actions/setup-node@v4
38+
with:
39+
node-version: ${{ matrix.node-version }}
40+
cache: 'yarn'
41+
42+
- name: Install dependencies
43+
run: yarn install --frozen-lockfile
44+
45+
- name: Build package
46+
run: yarn build
47+
48+
- name: Run all tests
49+
run: yarn test:all
50+
51+
- name: Check for TypeScript errors
52+
run: yarn build:ts
53+
54+
lint:
55+
runs-on: ubuntu-latest
56+
57+
steps:
58+
- name: Checkout
59+
uses: actions/checkout@v4
60+
61+
- name: Setup Node.js
62+
uses: actions/setup-node@v4
63+
with:
64+
node-version: '22'
65+
cache: 'yarn'
66+
67+
- name: Install dependencies
68+
run: yarn install --frozen-lockfile
69+
70+
- name: Build package
71+
run: yarn build
72+
73+
- name: Check TypeScript types
74+
run: yarn build:ts --noEmit

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
dist
2-
node_modules
2+
node_modules
3+
.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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!
44

5-
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/rishichawda/remark-notes-plugin/release.yml)
5+
![Test Status](https://img.shields.io/github/actions/workflow/status/rishichawda/remark-notes-plugin/test.yml?branch=main&label=tests)
66
![npm](https://img.shields.io/npm/v/remark-notes-plugin)
77
![License](https://img.shields.io/npm/l/remark-notes-plugin)
88
![Website](https://img.shields.io/website?url=https%3A%2F%2Frishichawda.github.io%2Fremark-notes-plugin)

__tests__/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Tests
2+
3+
## Test Structure
4+
5+
### Test Files
6+
7+
- **`index.ts`**: Fixture-based tests for plugin transformation
8+
- Tests each note type (note, tip, important, quote, bonus)
9+
- Compares actual output against known-good expected output
10+
- Uses `rehype-parse` to parse HTML into HAST for structural comparison
11+
- Strips position data for stable comparisons
12+
13+
- **`styles.ts`**: Tests for automatic style injection
14+
- Verifies styles are automatically injected
15+
- Ensures styles are injected only once per document
16+
- Validates CSS content is not empty
17+
18+
- **`generate-fixtures.ts`**: Utility script to regenerate expected outputs
19+
- Run when you make intentional changes to plugin output. MUST be done with caution.
20+
- Updates all fixture outputs to match current implementation
21+
22+
### Fixtures Directory (`__fixtures__/`)
23+
24+
Each note type has its own directory containing:
25+
26+
- `input.md`: Sample markdown input for that note type
27+
- `output.html`: Expected HTML output (auto-generated)
28+
29+
Structure:
30+
31+
```
32+
__fixtures__/
33+
├── note/
34+
│ ├── input.md
35+
│ └── output.html
36+
├── tip/
37+
│ ├── input.md
38+
│ └── output.html
39+
├── important/
40+
│ ├── input.md
41+
│ └── output.html
42+
├── quote/
43+
│ ├── input.md
44+
│ └── output.html
45+
└── bonus/
46+
├── input.md
47+
└── output.html
48+
```
49+
50+
## Running Tests
51+
52+
```bash
53+
# Run all tests
54+
yarn test
55+
56+
# Run fixture tests only
57+
yarn test:fixtures
58+
59+
# Run style injection tests only
60+
yarn test:styles
61+
62+
# Regenerate expected outputs after changes
63+
yarn generate:fixtures
64+
```
65+
66+
## Testing Approach
67+
68+
1. **Fixture-based testing**: Instead of inline test data, we use separate files for inputs and expected outputs
69+
2. **Structural comparison**: Uses `rehype-parse` to parse HTML into HAST (Hypertext Abstract Syntax Tree) for comparison
70+
3. **Position-agnostic**: Strips position data from HAST to avoid tests that break on whitespace changes
71+
4. **Separation of concerns**: Plugin transformation tests are separate from style injection tests
72+
73+
## Adding New Test Cases
74+
75+
To add a new test case:
76+
77+
1. Create a new directory in `__fixtures__/` (e.g., `__fixtures__/new-type/`)
78+
2. Add `input.md` with your test markdown
79+
3. Run `yarn generate:fixtures` to auto-generate the expected `output.html`
80+
4. Add the new type to the `FIXTURE_TYPES` array in `index.ts`
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
> [!bonus]
2+
> This is a bonus block.
3+
> Extra info for advanced users.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<blockquote class="remark-note remark-note-bonus"><div class="remark-note-header"><span class="remark-note-icon"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g stroke-linecap="round" stroke-linejoin="round"></g><g><path 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" stroke-width="1.5" stroke-linecap="round"></path><path d="M2.08887 16C3.20445 15.121 4.68639 14.7971 6.08887 15.1257" stroke-width="1.5" stroke-linecap="round"></path><path d="M2.08887 10.5C3.08887 10 3.37862 10.0605 4.08887 10" stroke-width="1.5" stroke-linecap="round"></path><path d="M2 5.60867L2.20816 5.48676C4.41383 4.19506 6.75032 3.84687 8.95304 4.48161L9.16092 4.54152" stroke-linecap="round" stroke-width="1.5"></path></g></svg></span><span class="remark-note-title">bonus</span></div><div class="remark-note-content"><p>This is a bonus block.
2+
Extra info for advanced users.</p></div></blockquote>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
> [!important]
2+
> This is an important block.
3+
> Pay attention to this!
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<blockquote class="remark-note remark-note-important"><div class="remark-note-header"><span class="remark-note-icon"><svg viewBox="0 0 18 18" xmlns="http://www.w3.org/2000/svg" fill="currentColor"><g stroke-width="0"></g><g stroke-linecap="round" stroke-linejoin="round"></g><g><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><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><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></g></svg></span><span class="remark-note-title">important</span></div><div class="remark-note-content"><p>This is an important block.
2+
Pay attention to this!</p></div></blockquote>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
> [!note]
2+
> This is a note block.
3+
> It supports **bold** and *italic* text.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<blockquote class="remark-note remark-note-note"><div class="remark-note-header"><span class="remark-note-icon"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g stroke-linecap="round" stroke-linejoin="round" stroke-width="0.048"></g><g><path d="M8 2V5" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"></path><path d="M16 2V5" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"></path><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" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"></path><path d="M8 11H16" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"></path><path d="M8 16H12" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"></path></g></svg></span><span class="remark-note-title">note</span></div><div class="remark-note-content"><p>This is a note block.
2+
It supports <strong>bold</strong> and <em>italic</em> text.</p></div></blockquote>

0 commit comments

Comments
 (0)