Skip to content

Commit

Permalink
refactor(packages/remark-tight-comments): add new package
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Oct 20, 2022
1 parent 60b50c2 commit cb8c620
Show file tree
Hide file tree
Showing 22 changed files with 1,041 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/remark-tight-comments/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Bernard Dickens

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.
221 changes: 221 additions & 0 deletions packages/remark-tight-comments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<!-- prettier-ignore-start -->
<!-- badges-start -->

[![Black Lives Matter!][badge-blm]][link-blm]
[![Maintenance status][badge-maintenance]][link-repo]
[![Last commit timestamp][badge-last-commit]][link-repo]
[![Open issues][badge-issues]][link-issues]
[![Pull requests][badge-pulls]][link-pulls]
[![Codecov][badge-codecov]][link-codecov]
[![Source license][badge-license]][link-license]
[![NPM version][badge-npm]][link-npm]
[![Uses Semantic Release!][badge-semantic-release]][link-semantic-release]

<!-- badges-end -->
<!-- prettier-ignore-end -->

# remark-tight-comments

This is a [unified][23] ([remark][24]) plugin that allows you to selectively
remove unnecessary newlines around Markdown comments.

This plugin is useful for preserving the structure of auto-generated content,
e.g. [all-contributors][12], [doctoc][1], etc. You might also be interested in
[remark-ignore][26], which lets you instruct remark not to transform parts of
your Markdown documents. For a live example of these two plugins in action,
check the source of [this very README.md file][25]. ✨

---

<!-- prettier-ignore-start -->
<!-- remark-tight-comments-start -->
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Install][3]
- [Usage][4]
- [Via API][13]
- [Via remark-cli][14]
- [Via unified configuration][15]
- [API][5]
- [Related][7]
- [Contributing and Support][8]
- [Contributors][9]

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- remark-tight-comments-end -->
<!-- prettier-ignore-end -->

## Install

> Due to the nature of the unified ecosystem, this package is ESM only and
> cannot be `require`'d.
```bash
npm install --save-dev remark-tight-comments
```

## Usage

### Via API

> See [mdast-util-tight-comments][27] for more details.
```typescript
import { read } from 'to-vfile';
import { remark } from 'remark';
import remarkTightComments from 'remark-tight-comments';

const file = await remark()
.use(remarkTightComments)
.process(await read('example.md'));

console.log(String(file));
```

<!-- remark-ignore -->

### Via [remark-cli](https://github.com/remarkjs/remark/tree/main/packages/remark-cli)

```shell
remark -o --use tight-comments README.md
```

<!-- remark-ignore -->

### Via [unified configuration](https://github.com/unifiedjs/unified-engine/blob/main/doc/configure.md)

In `package.json`:

```json
/* … */
"remarkConfig": {
"plugins": [
"remark-tight-comments"
/* … */
]
},
/* … */
```

In `.remarkrc.js`:

```javascript
module.exports = {
plugins: [
//
'tight-comments'
]
};
```

In `.remarkrc.mjs`:

```javascript
import remarkTightComments from 'remark-tight-comments';

export default {
plugins: [
//
remarkTightComments
]
};
```

## API

Detailed interface information can be found under [`docs/`][docs].

## Related

- [remark-ignore][26] — use comments to exclude one or more nodes from
[transformation][28]
- [mdast-util-tight-comments][29] — selectively remove newlines around comment
nodes during serialization

## Contributing and Support

**[New issues][choose-new-issue] and [pull requests][pr-compare] are always
welcome and greatly appreciated! 🤩** Just as well, you can [star 🌟 this
project][link-repo] to let me know you found it useful! ✊🏿 Thank you!

See [CONTRIBUTING.md][contributing] and [SUPPORT.md][support] for more
information.

### Contributors

<!-- TODO: all-contributors here -->

[badge-blm]: https://xunn.at/badge-blm 'Join the movement!'
[link-blm]: https://xunn.at/donate-blm
[badge-maintenance]:
https://img.shields.io/maintenance/active/2022
'Is this package maintained?'
[link-repo]:
https://github.com/xunnamius/unified-utils/blob/main/packages/remark-tight-comments
[badge-last-commit]:
https://img.shields.io/github/last-commit/xunnamius/unified-utils
'Latest commit timestamp'
[badge-issues]:
https://img.shields.io/github/issues/Xunnamius/unified-utils
'Open issues'
[link-issues]: https://github.com/Xunnamius/unified-utils/issues?q=
[badge-pulls]:
https://img.shields.io/github/issues-pr/xunnamius/unified-utils
'Open pull requests'
[link-pulls]: https://github.com/xunnamius/unified-utils/pulls
[badge-codecov]:
https://codecov.io/gh/Xunnamius/unified-utils/branch/main/graph/badge.svg?token=HWRIOBAAPW
'Is this package well-tested?'
[link-codecov]: https://codecov.io/gh/Xunnamius/unified-utils
[badge-license]:
https://img.shields.io/npm/l/remark-tight-comments
"This package's source license"
[link-license]:
https://github.com/Xunnamius/unified-utils/blob/main/packages/remark-tight-comments/LICENSE
[badge-npm]:
https://api.ergodark.com/badges/npm-pkg-version/remark-tight-comments
'Install this package using npm or yarn!'
[link-npm]: https://www.npmjs.com/package/remark-tight-comments
[badge-semantic-release]:
https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
'This repo practices continuous integration and deployment!'
[link-semantic-release]: https://github.com/semantic-release/semantic-release
[package-json]: package.json
[docs]: docs
[choose-new-issue]: https://github.com/xunnamius/unified-utils/issues/new/choose
[pr-compare]: https://github.com/xunnamius/unified-utils/compare
[contributing]: /CONTRIBUTING.md
[support]: /.github/SUPPORT.md
[1]: https://github.com/thlorenz/doctoc
[2]: #remark-tight-comments
[3]: #install
[4]: #usage
[5]: #api
[6]: #examples
[7]: #related
[8]: #contributing-and-support
[9]: #contributors
[10]: https://prettier.io/docs/en/ignore.html#javascript
[11]: https://github.com/unifiedjs/unified#processoruseplugin-options
[12]: https://github.com/all-contributors/all-contributors
[13]: #via-api
[14]: #via-remark-cli
[15]: #via-unified-configuration
[16]: https://github.com/remarkjs/remark/tree/main/packages/remark-cli
[17]: https://github.com/unifiedjs/unified-engine/blob/main/doc/configure.md
[18]:
https://github.com/zestedesavoir/zmarkdown/tree/HEAD/packages/remark-comments#readme
[19]:
https://github.com/zestedesavoir/zmarkdown/tree/HEAD/packages/remark-disable-tokenizers#readme
[20]: https://github.com/remarkjs/remark-message-control
[21]: https://github.com/syntax-tree/mdast-comment-marker
[22]: https://github.com/syntax-tree/mdast-zone
[23]: https://github.com/unifiedjs/unified
[24]: https://github.com/remarkjs/remark
[25]:
https://raw.githubusercontent.com/Xunnamius/unified-utils/main/packages/remark-tight-comments/README.md
[26]: /packages/remark-ignore
[27]: /packages/mdast-util-tight-comments/README.md#usage
[28]: https://github.com/unifiedjs/unified#overview
[29]: /packages/mdast-util-tight-comments
97 changes: 97 additions & 0 deletions packages/remark-tight-comments/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "remark-tight-comments",
"version": "0.0.0-semantic",
"description": "remark plugin to selectively remove newlines around comments",
"keywords": [
"unified",
"mdast",
"remark",
"remark-plugin",
"plugin",
"markdown",
"comment",
"space",
"node",
"newline",
"remove",
"selective"
],
"homepage": "https://github.com/Xunnamius/unified-utils/blob/main/packages/remark-tight-comments",
"repository": {
"type": "git",
"url": "https://github.com/Xunnamius/unified-utils"
},
"license": "MIT",
"author": "Xunnamius",
"sideEffects": false,
"type": "commonjs",
"exports": {
"./start": {
"types": "./dist/start.d.ts",
"node": "./dist/start.mjs",
"default": "./dist/start.mjs"
},
"./end": {
"types": "./dist/end.d.ts",
"node": "./dist/end.mjs",
"default": "./dist/end.mjs"
},
".": {
"types": "./dist/index.d.ts",
"node": "./dist/index.mjs",
"default": "./dist/index.mjs"
},
"./package": "./package.json",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"start": [
"./dist/start.d.ts"
],
"end": [
"./dist/end.d.ts"
],
"*": [
"./dist/index.d.ts"
]
}
},
"files": [
"/dist",
"/LICENSE",
"/package.json",
"/README.md"
],
"scripts": {
"build": "npm run build:dist --",
"build:changelog": "conventional-changelog --outfile CHANGELOG.md --config ../../conventional.config.js --release-count 0 --skip-unstable && (if [ \"$CHANGELOG_SKIP_TITLE\" != 'true' ]; then { node -e 'console.log(require(\"../../conventional.config.js\").changelogTitle)'; cat CHANGELOG.md; } > CHANGELOG.md.ignore && mv CHANGELOG.md.ignore CHANGELOG.md; fi) && NODE_ENV=format remark --output --frail CHANGELOG.md && prettier --write CHANGELOG.md",
"build:dist": "NODE_ENV=production tsc --project tsconfig.types.json --incremental false && tsconfig-replace-paths --project tsconfig.types.json && NODE_ENV=production-esm babel src --extensions .ts --out-dir dist --out-file-extension .mjs --root-mode upward",
"build:docs": "if [ -r ./next.config.js ]; then typedoc --plugin typedoc-plugin-markdown --tsconfig tsconfig.docs.json --out docs --readme none lib src test types external-scripts --exclude '**/*.test.*' --exclude external-scripts/bin; else ENTRY=`node -e 'const entry = require(\"./package.json\").config?.[\"plugin-build\"]?.docs?.entry; if(!entry) throw new Error(\"\\\"config['\"'\"'plugin-build'\"'\"'].docs.entry\\\" field is not defined in package.json\"); console.log(entry)'` && echo 'Entry file:' \"$ENTRY\" && typedoc --plugin typedoc-plugin-markdown --tsconfig tsconfig.docs.json --out docs --readme none $(echo $ENTRY) && find docs -name '*.md' -exec sed -i -e 's/Project: //g' {} + && sed -i -e 1,4d docs/README.md; fi && find docs -name '*.md' -exec sed -i -e 's/`__namedParameters`/`\\(destructured\\)`/g' {} + && find docs -name '*.md' -exec sed -i -E 's/`__namedParameters\\.([^`]+)`/`\\({ \\1 }\\)`/g' {} +",
"clean": "git ls-files --exclude-standard --ignored --others --directory | grep -vE '^((\\.(env|vscode|husky))|next-env\\.d\\.ts|node_modules)($|\\/)' | xargs -p rm -rf",
"format": "cd ../.. && npm run format",
"lint": "echo 'IMPLEMENT ME'",
"list-tasks": "node -e 'console.log(Object.keys(require(\"./package.json\").scripts).join(\"\\n\"))'",
"test": "npm run test:unit --",
"test:integration": "echo 'IMPLEMENT ME'",
"test:unit": "echo 'IMPLEMENT ME'"
},
"config": {
"plugin-build": {
"docs": {
"entry": "./src/*"
}
}
},
"dependencies": {
"mdast-comment-marker": "^2.1.0",
"mdast-util-hidden": "^1.0.0",
"unist-util-visit": "^4.1.1"
},
"engines": {
"node": "^14.19.0 || ^16.13.0 || >=17.4.0"
},
"publishConfig": {
"access": "public"
}
}
23 changes: 23 additions & 0 deletions packages/remark-tight-comments/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { joinTightComments } from 'mdast-util-tight-comments';

import type { Plugin } from 'unified';
import type { Root } from 'mdast';

/**
* A remark plugin that ensures tight spacing between HTML comments and select
* other mdast nodes under certain conditions.
*/
const remarkTightComments: Plugin<void[], Root> = function () {
const data = this.data();

add('toMarkdownExtensions', joinTightComments());

function add(field: string, value: unknown) {
const list = // ? Be cognizant of other extensions
(data[field] ? data[field] : (data[field] = [])) as unknown[];

list.push(value);
}
};

export default remarkTightComments;
15 changes: 15 additions & 0 deletions packages/remark-tight-comments/test/fixtures/clean-transformed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Some project

<!-- remark some comment -->

[![Build][2]][1]

## Section

[A link][3]

[Another link][3]

[1]: https://github.com/remarkjs/remark-defsplit/actions
[2]: https://github.com/remarkjs/remark-defsplit/workflows/main/badge.svg
[3]: https://example.com
15 changes: 15 additions & 0 deletions packages/remark-tight-comments/test/fixtures/clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Some project

<!-- remark some comment -->

[![Build][2]][1]

## Section

[A link][3]

[Another link][3]

[1]: https://github.com/remarkjs/remark-defsplit/actions
[2]: https://github.com/remarkjs/remark-defsplit/workflows/main/badge.svg
[3]: https://example.com
Loading

0 comments on commit cb8c620

Please sign in to comment.