Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions .babelrc

This file was deleted.

9 changes: 0 additions & 9 deletions .eslintrc

This file was deleted.

67 changes: 67 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: CI

on:
push:
branches:
- master
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Use Node.js 24
uses: actions/setup-node@v6
with:
node-version: 24
cache: "yarn"
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Lint
run: yarn lint
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Use Node.js 24
uses: actions/setup-node@v6
with:
node-version: 24
cache: "yarn"
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Check types
run: yarn check-build
- name: Build
run: yarn build
- name: Load the package both ways
run: |
node --input-type=module -e "import CensorifyIt from 'censorify-it'; new CensorifyIt().forbidden('evil.cfd/a/x')"
node -e "const { default: CensorifyIt } = require('censorify-it'); new CensorifyIt().forbidden('evil.cfd/a/x')"
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Use Node.js 24
uses: actions/setup-node@v6
with:
node-version: 24
cache: "yarn"
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Test with coverage
run: yarn coverage
env:
CI: true
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v7
with:
token: ${{ secrets.CODECOV_TOKEN }}
report_type: coverage
- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/codecov-action@v7
with:
token: ${{ secrets.CODECOV_TOKEN }}
report_type: test_results
76 changes: 0 additions & 76 deletions .github/workflows/nodejs.yml

This file was deleted.

12 changes: 8 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# dependencies
/node_modules

# build and test outputs
/dist
/coverage
coverage.lcov
test-report.junit.xml
.eslintcache
*.tsbuildinfo

# misc
.DS_Store

npm-debug.log*
yarn-debug.log*
yarn-error.log*

dist/*

.eslintcache
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx commitlint --edit
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx lint-staged --allow-empty $1
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Built as an extension of [LinkifyIt](https://github.com/markdown-it/linkify-it)
yarn add censorify-it
```

The package is written in TypeScript and ships its types. It is an ES module, with a CommonJS build:

```js
import CensorifyIt from 'censorify-it' // or: import { CensorifyIt } from 'censorify-it'
const { CensorifyIt } = require('censorify-it')
```

## Usage

```js
Expand Down Expand Up @@ -110,4 +117,48 @@ console.log(censorify.match('Check out github.com or mysite.com or even example.
// ]
```

Every match has a `type`: `link`, `email` or `phone`. Exceptions can be given by type, which is safer than a single
list: a loose phone number pattern in a single list also lets through the links holding these digits.

```js
censorify.set({
exceptions: {
link: [/^(?:https?:\/\/)?(?:[\w-]+\.)*mysite\.com(?=[/?#]|$)/i],
phone: [/\+33.?1.?23.?45.?67.?89/],
},
})
```

## Refusing a text instead of censoring it

Censoring can afford false positives: a date taken for a phone number only costs a few stars. Refusing a text
cannot. `forbidden` only reports what is a link or a phone number beyond doubt, exceptions applied:

- links: `scheme://`, `www.`, `domain/path` or `domain?query`, whatever the TLD. A bare domain is not reported,
it would also catch a missing space after a period (`see you soon.In any case`);
- phone numbers: international ones (`+…`, `00…`) from 8 digits, national ones from 9 digits. Dates and lists of
small numbers (`12 15 20 25 30 people`) are left aside.

```js
const censorify = new CensorifyIt()

console.log(censorify.forbidden('Go to evil.cfd/a/x or call 06 12 34 56 78, we are 12 on 2026-11-20'))
// [
// Match { schema: '', type: 'link', index: 6, lastIndex: 18, raw: 'evil.cfd/a/x', url: 'http://evil.cfd/a/x', text: 'evil.cfd/a/x' },
// Match { schema: '', type: 'phone', index: 27, lastIndex: 41, raw: '06 12 34 56 78', url: '06 12 34 56 78', text: '06 12 34 56 78' }
// ]
```

Everything `forbidden` reports is censored by `process` as well.

All the other settings and additions as defined by [LinkifyIt](https://github.com/markdown-it/linkify-it/blob/master/README.md#api) also apply.

## Development

```
yarn test # vitest, in watch mode
yarn coverage # one run, with coverage
yarn lint
yarn check-build # type check
yarn build # vite, to dist/
```
3 changes: 3 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
extends: ['@eatwith/commitlint-config-conventional'],
}
32 changes: 32 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { defineConfig, globalIgnores } from 'eslint/config'
import globals from 'globals'
import { typescript } from 'eslint-config-vizeat'

export default defineConfig(
globalIgnores([
'node_modules',
'dist',
'coverage',
'lint-staged.config.js',
'vite.config.ts',
'commitlint.config.js',
]),
typescript,
{
languageOptions: {
sourceType: 'module',
globals: {
...globals.node,
...globals.browser,
...globals.vitest,
},
parserOptions: {
project: './tsconfig.eslint.json',
},
},
files: ['src/**/*.ts'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
},
},
)
4 changes: 4 additions & 0 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
'**/*.{js,ts}': 'eslint --fix',
'**/*.{js,ts}?(x)': () => 'yarn check-build',
}
Loading
Loading