-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0722669
Showing
14 changed files
with
9,035 additions
and
0 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,4 @@ | ||
{ | ||
"100": true, | ||
"reporter": ["html", "lcov", "text"] | ||
} |
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,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 |
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 @@ | ||
root: true | ||
extends: | ||
- remcohaszing | ||
rules: | ||
no-invalid-this: off | ||
no-proto: off | ||
import/no-extraneous-dependencies: off | ||
jsdoc/check-indentation: off |
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,102 @@ | ||
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: | ||
- 18 | ||
- 20 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- 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 }} |
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,6 @@ | ||
coverage/ | ||
node_modules/ | ||
*.d.ts | ||
*.log | ||
*.map | ||
*.tgz |
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 @@ | ||
proseWrap: always | ||
semi: false | ||
singleQuote: true | ||
trailingComma: none |
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,2 @@ | ||
plugins: | ||
- remark-preset-remcohaszing |
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,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. |
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,76 @@ | ||
# recma-export-filepath | ||
|
||
[![github actions](https://github.com/remcohaszing/recma-export-filepath/actions/workflows/ci.yaml/badge.svg)](https://github.com/remcohaszing/recma-export-filepath/actions/workflows/ci.yaml) | ||
[![codecov](https://codecov.io/gh/remcohaszing/recma-export-filepath/branch/main/graph/badge.svg)](https://codecov.io/gh/remcohaszing/recma-export-filepath) | ||
[![npm version](https://img.shields.io/npm/v/recma-export-filepath)](https://www.npmjs.com/package/recma-export-filepath) | ||
[![npm downloads](https://img.shields.io/npm/dm/recma-export-filepath)](https://www.npmjs.com/package/recma-export-filepath) | ||
|
||
A recma plugin to expose the filepath as a named export. | ||
|
||
## Table of Contents | ||
|
||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [API](#api) | ||
- [Options](#options) | ||
- [Compatibility](#compatibility) | ||
- [License](#license) | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install recma-export-filepath | ||
``` | ||
|
||
## Usage | ||
|
||
This recma plugin exposes the filepath of the current file as a named export. | ||
|
||
For example, given a file named `example.mdx` with the following contents: | ||
|
||
```mdx | ||
Hello 👋 | ||
``` | ||
|
||
The following script: | ||
|
||
```js | ||
import { compile } from '@mdx-js/mdx' | ||
import recmaExportFilepath from 'recma-export-filepath' | ||
import { read } from 'to-vfile' | ||
|
||
const { contents } = await compile(await read('example.mdx'), { | ||
jsx: true, | ||
recmaPlugins: [recmaExportFilepath] | ||
}) | ||
|
||
console.log(contents) | ||
``` | ||
|
||
Roughly yields: | ||
|
||
```jsx | ||
export const filepath = 'example.mdx' | ||
|
||
export default function MDXContent() { | ||
return <p>Hello 👋</p> | ||
} | ||
``` | ||
|
||
## API | ||
|
||
The default export is a recma plugin. | ||
|
||
### Options | ||
|
||
- `name` (`string`, default: `'filepath'`) — The name to export the file path as. | ||
- `absolute` (`boolean`, default: `false`) — If true, use an absolute path. By default a relative | ||
path is used. | ||
|
||
## Compatibility | ||
|
||
This project is compatible with Node.js 16 or greater. | ||
|
||
## License | ||
|
||
[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing) |
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,59 @@ | ||
import { relative } from 'node:path' | ||
|
||
import { name as isIdentifierName } from 'estree-util-is-identifier-name' | ||
import normalizePath from 'normalize-path' | ||
|
||
/** | ||
* @typedef RecmaExportFilepathOptions | ||
* @property {boolean} [absolute=false] | ||
* If true, use an absolute path. By default a relative path is used. | ||
* @property {string} [name='filepath'] | ||
* The name to export the file path as. | ||
*/ | ||
|
||
/** | ||
* A recma plugin to expose the filepath as a named export | ||
* | ||
* @type {import('unified').Plugin<[RecmaExportFilepathOptions?], import('estree').Program>} | ||
*/ | ||
export default function recmaExportFilepath({ absolute = false, name = 'filepath' } = {}) { | ||
if (!isIdentifierName(name)) { | ||
throw new Error(`Name this should be a valid identifier, got: ${JSON.stringify(name)}`) | ||
} | ||
|
||
return (ast, file) => { | ||
let value = file.path | ||
|
||
if (value) { | ||
const cwd = normalizePath(file.cwd) | ||
value = normalizePath(value) | ||
|
||
if (!absolute) { | ||
value = relative(cwd, value) | ||
} | ||
} else { | ||
const message = file.message('Missing file.path', { | ||
source: 'recma-export-filepath', | ||
ruleId: 'missing-filepath' | ||
}) | ||
message.url = 'https://github.com/remcohaszing/recma-export-filepath' | ||
value = '' | ||
} | ||
|
||
ast.body.unshift({ | ||
type: 'ExportNamedDeclaration', | ||
specifiers: [], | ||
declaration: { | ||
type: 'VariableDeclaration', | ||
kind: 'const', | ||
declarations: [ | ||
{ | ||
type: 'VariableDeclarator', | ||
id: { type: 'Identifier', name }, | ||
init: { type: 'Literal', value } | ||
} | ||
] | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.