Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Aug 31, 2023
0 parents commit 2c439e5
Show file tree
Hide file tree
Showing 13 changed files with 9,307 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 100
trim_trailing_whitespace = true

[COMMIT_EDITMSG]
max_line_length = 72
5 changes: 5 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root: true
extends:
- remcohaszing
rules:
no-use-before-define: off
104 changes: 104 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: ci

on:
pull_request:
push:
branches: [main]
tags: ['*']

jobs:
eslint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
- run: npm ci
- run: npx eslint .

pack:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
- run: npm ci
- run: npm pack
- uses: actions/upload-artifact@v3
with:
name: package
path: '*.tgz'

test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 16
- 18
- 20
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install --global npm@latest
- run: npm ci
- run: npm test
- uses: codecov/codecov-action@v3
if: ${{ matrix.node-version == 20 }}

prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
- run: npm ci
- run: npx prettier --check .

remark:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
- run: npm ci
- run: npx remark --frail .

tsc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
- run: npm ci
- run: npx tsc

release:
runs-on: ubuntu-latest
needs:
- eslint
- test
- pack
- prettier
- remark
- tsc
if: startsWith(github.ref, 'refs/tags/')
permissions:
id-token: write
steps:
- uses: actions/setup-node@v3
with:
node-version: 20
registry-url: https://registry.npmjs.org
- uses: actions/download-artifact@v3
with: { name: package }
- run: npm publish *.tgz --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage/
dist/
node_modules/
*.d.ts
*.log
*.map
*.tgz
*.tsbuildinfo
4 changes: 4 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
proseWrap: always
semi: false
singleQuote: true
trailingComma: none
2 changes: 2 additions & 0 deletions .remarkrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plugins:
- remark-preset-remcohaszing
18 changes: 18 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# MIT License

Copyright © 2023 Remco Haszing

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# recma-mdx-is-mdx-component

[![github actions](https://github.com/remcohaszing/recma-mdx-is-mdx-component/actions/workflows/ci.yaml/badge.svg)](https://github.com/remcohaszing/recma-mdx-is-mdx-component/actions/workflows/ci.yaml)
[![npm version](https://img.shields.io/npm/v/recma-mdx-is-mdx-component)](https://www.npmjs.com/package/recma-mdx-is-mdx-component)
[![npm downloads](https://img.shields.io/npm/dm/recma-mdx-is-mdx-component)](https://www.npmjs.com/package/recma-mdx-is-mdx-component)
[![codecov](https://codecov.io/gh/remcohaszing/recma-mdx-is-mdx-component/branch/main/graph/badge.svg)](https://codecov.io/gh/remcohaszing/recma-mdx-is-mdx-component)

A recma plugin to define the `isMdxComponent` property on MDX components.

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [API](#api)
- [Compatibility](#compatibility)
- [License](#license)

## Installation

```sh
npm install recma-mdx-is-mdx-component
```

## Usage

This recma plugin assigns `true` to the property `MDXContent.isMdxComponent`.

For example, given a file named `example.mdx` with the following contents:

```mdx
Some MDX content
```

The following script:

```js
import { readFile } from 'node:fs/promises'

import { compile } from '@mdx-js/mdx'
import recmaPluginInjectIsMdxComponent from 'recma-mdx-is-mdx-component'

const { contents } = await compile(await readFile('example.mdx'), {
jsx: true,
recmaPlugins: [recmaPluginInjectIsMdxComponent]
})
console.log(contents)
```

Roughly yields:

```jsx
MDXContent.isMdxComponent = true
export default function MDXContent() {
return <p>Some MDX content</p>
}
```

## API

The default export is a recma plugin. It takes no options.

## Compatibility

This project is compatible with Node.js 16 or greater.

## License

[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Inserty a conditional assignment of `MDXContent.isMdxComponent = true` in the AST.
*
* @param {import('estree').Program} ast
* The AST to transform.
* @returns {undefined}
*/
function transformer(ast) {
// This needs to be defined before the return statement if the user uses evaluate()
ast.body.unshift({
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'MemberExpression',
object: { type: 'Identifier', name: 'MDXContent' },
property: { type: 'Identifier', name: 'isMdxComponent' },
computed: false,
optional: false
},
right: { type: 'Literal', value: true }
}
})
}

/**
* @type {import('unified').Plugin<[], import('estree').Program>}
*
* A recma plugin to define the `isMdxComponent` property on MDX components.
*/
export const recmaPluginInjectIsMdxComponent = () => transformer
Loading

0 comments on commit 2c439e5

Please sign in to comment.