Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Oct 12, 2023
0 parents commit 0722669
Show file tree
Hide file tree
Showing 14 changed files with 9,035 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"100": true,
"reporter": ["html", "lcov", "text"]
}
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
8 changes: 8 additions & 0 deletions .eslintrc.yaml
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
102 changes: 102 additions & 0 deletions .github/workflows/ci.yaml
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 }}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage/
node_modules/
*.d.ts
*.log
*.map
*.tgz
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.
76 changes: 76 additions & 0 deletions README.md
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)
59 changes: 59 additions & 0 deletions index.js
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 }
}
]
}
})
}
}
Loading

0 comments on commit 0722669

Please sign in to comment.