-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add button directive Co-authored-by: Angus Hollands <[email protected]> * Update index.ts * Change from directive to role * Delete changelog * Fix linting * Remove test * Add buttonRoles test * refactor: use 'class' exclusively for buttons * chore: add changeset * Update doc * Add buttonRole to directives plugin * Add buttonRole to roles reference * Add button docs * Typo * Typo * Minor edit * docs: use MyST directive for extensions * docs: use myst:role instead of myst:directive * fix: correct heading depth --------- Co-authored-by: Angus Hollands <[email protected]> Co-authored-by: Angus Hollands <[email protected]>
- Loading branch information
1 parent
3b4a5f8
commit fad8f67
Showing
14 changed files
with
171 additions
and
36 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"myst-ext-button": patch | ||
"myst-spec-ext": patch | ||
"myst-cli": patch | ||
--- | ||
|
||
Add new button role |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['curvenote'], | ||
}; |
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,3 @@ | ||
# myst-ext-button | ||
|
||
`mystmd` extension for `button` role |
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,41 @@ | ||
{ | ||
"name": "myst-ext-button", | ||
"version": "0.0.0", | ||
"sideEffects": false, | ||
"license": "MIT", | ||
"description": "MyST extension for button role", | ||
"author": "Jenny Wong <[email protected]>", | ||
"homepage": "https://github.com/jupyter-book/mystmd/tree/main/packages/myst-ext-button", | ||
"type": "module", | ||
"exports": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/jupyter-book/mystmd.git" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf dist", | ||
"lint": "eslint \"src/**/!(*.spec).ts\" -c ./.eslintrc.cjs", | ||
"lint:format": "npx prettier --check \"src/**/*.ts\"", | ||
"test": "vitest run", | ||
"test:watch": "vitest watch", | ||
"build:esm": "tsc", | ||
"build": "npm-run-all -l clean -p build:esm" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/jupyter-book/mystmd/issues" | ||
}, | ||
"dependencies": { | ||
"myst-common": "^1.7.2", | ||
"myst-spec-ext": "^1.7.6" | ||
}, | ||
"devDependencies": { | ||
"myst-parser": "^1.5.7" | ||
} | ||
} |
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,28 @@ | ||
import type { RoleSpec, RoleData, GenericNode } from 'myst-common'; | ||
import type { Link } from 'myst-spec-ext'; | ||
|
||
const REF_PATTERN = /^(.+?)<([^<>]+)>$/; | ||
|
||
export const buttonRole: RoleSpec = { | ||
name: 'button', | ||
doc: 'Button element with an action to navigate to internal or external links.', | ||
body: { | ||
type: String, | ||
doc: 'The body of the button.', | ||
required: true, | ||
}, | ||
run(data: RoleData): GenericNode[] { | ||
const body = data.body as string; | ||
const match = REF_PATTERN.exec(body); | ||
const [, modified, rawLabel] = match ?? []; | ||
const url = rawLabel ?? body; | ||
const node: Link = { | ||
type: 'link', | ||
url, | ||
children: [], | ||
class: 'button', // TODO: allow users to extend this | ||
}; | ||
if (modified) node.children = [{ type: 'text', value: modified.trim() }]; | ||
return [node]; | ||
}, | ||
}; |
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,33 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { buttonRole } from '../src'; | ||
import { VFile } from 'vfile'; | ||
|
||
describe('Button component', () => { | ||
it('should process button role correctly', () => { | ||
const result = buttonRole.run( | ||
{ name: 'button', body: 'Click me<http://example.com>' }, | ||
new VFile(), | ||
); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
type: 'link', | ||
class: 'button', | ||
url: 'http://example.com', | ||
children: [{ type: 'text', value: 'Click me' }], | ||
}, | ||
]); | ||
}); | ||
|
||
it('should process button role without label correctly', () => { | ||
const result = buttonRole.run({ name: 'button', body: 'http://example.com' }, new VFile()); | ||
expect(result).toEqual([ | ||
{ | ||
type: 'link', | ||
class: 'button', | ||
url: 'http://example.com', | ||
children: [], | ||
}, | ||
]); | ||
}); | ||
}); |
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 @@ | ||
{ | ||
"extends": "../tsconfig/base.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
}, | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules", "src/**/*.spec.ts", "tests"] | ||
} |
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