-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Escape markdown for dynamic values (#826)
- Loading branch information
1 parent
49084f8
commit 7c0be21
Showing
16 changed files
with
986 additions
and
758 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,90 @@ | ||
{ | ||
"root": true, | ||
"env": { | ||
"browser": true, | ||
"es2021": true | ||
}, | ||
"extends": [ | ||
"plugin:import/typescript", | ||
"prettier", | ||
"plugin:jest-dom/recommended" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"ignorePatterns": [ | ||
"build/*", | ||
"dist/*", | ||
"webpack.config.js", | ||
"bundle/*", | ||
"coverage/*", | ||
"jest.config.cjs", | ||
"rollup.config*.js" | ||
], | ||
"parserOptions": { | ||
"ecmaFeatures": { | ||
"jsx": true | ||
}, | ||
"ecmaVersion": "latest", | ||
"sourceType": "module", | ||
"project": "./tsconfig.json", | ||
"tsconfigRootDir": "./" | ||
}, | ||
"plugins": [ | ||
"@typescript-eslint", | ||
"prettier", | ||
"import", | ||
"prefer-arrow", | ||
"jest-dom", | ||
"jest", | ||
"jest-formatting", | ||
"no-only-tests" | ||
], | ||
"settings": { | ||
"import/parsers": { | ||
"@typescript-eslint/parser": [".ts", ".tsx"] | ||
}, | ||
"import/resolver": { | ||
"typescript": { | ||
"alwaysTryTypes": true | ||
} | ||
} | ||
}, | ||
"rules": { | ||
"no-tabs": ["error", { "allowIndentationTabs": true }], | ||
"@typescript-eslint/indent": ["off"], | ||
"quotes": [ | ||
"error", | ||
"single", | ||
{ "avoidEscape": true, "allowTemplateLiterals": true } | ||
], | ||
"@typescript-eslint/quotes": [ | ||
"error", | ||
"single", | ||
{ "avoidEscape": true, "allowTemplateLiterals": true } | ||
], | ||
"@typescript-eslint/comma-dangle": ["off"], | ||
"comma-dangle": ["off"], | ||
"no-console": 2, | ||
"no-only-tests/no-only-tests": 2, | ||
"no-warning-comments": 2, | ||
"import/no-unresolved": 2, | ||
"import/named": 2, | ||
"import/no-relative-packages": 2, | ||
"import/no-cycle": 2, | ||
"import/newline-after-import": 2, | ||
"import/no-namespace": 2, | ||
"import/no-duplicates": 2, | ||
"import/first": 2, | ||
"import/exports-last": 2, | ||
"import/no-absolute-path": 2, | ||
"import/no-dynamic-require": 2, | ||
"import/no-self-import": 2, | ||
"import/no-useless-path-segments": 2, | ||
"import/prefer-default-export": 0, | ||
"import/no-extraneous-dependencies": [ | ||
2, | ||
{ | ||
"devDependencies": ["!./src/**/*"] | ||
} | ||
] | ||
} | ||
} |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Descope <help@descope.com> | ||
|
||
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. |
Empty file.
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,33 @@ | ||
const { pathsToModuleNameMapper } = require('ts-jest'); | ||
const { compilerOptions } = require('./tsconfig.json'); | ||
|
||
module.exports = { | ||
clearMocks: true, | ||
|
||
collectCoverage: true, | ||
coverageDirectory: 'coverage', | ||
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'], | ||
coverageThreshold: { | ||
global: { | ||
branches: 100, | ||
functions: 100, | ||
lines: 100, | ||
statements: 100, | ||
}, | ||
}, | ||
// A set of global variables that need to be available in all test environments | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: 'tsconfig.json', | ||
}, | ||
BUILD_VERSION: 'one.two.three', | ||
}, | ||
|
||
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
moduleDirectories: ['node_modules', 'src'], | ||
|
||
testTimeout: 2000, | ||
|
||
roots: ['src', 'test'], | ||
}; |
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 @@ | ||
{ | ||
"name": "@descope/escape-markdown", | ||
"version": "0.0.1", | ||
"author": "Descope Team <info@descope.com>", | ||
"homepage": "https://github.com/descope/descope-js", | ||
"bugs": { | ||
"url": "https://github.com/descope/descope-js/issues", | ||
"email": "help@descope.com" | ||
}, | ||
"main": "dist/cjs/index.cjs.js", | ||
"module": "dist/index.esm.js", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
"require": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/cjs/index.cjs.js" | ||
}, | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.esm.js" | ||
} | ||
}, | ||
"type": "module", | ||
"description": "Markdown escaping utility", | ||
"scripts": { | ||
"build": "rimraf dist && rollup -c", | ||
"test": "jest", | ||
"format": "prettier . -w --ignore-path .gitignore", | ||
"format-check": "prettier . --check --ignore-path .gitignore", | ||
"lint": "eslint '+(src|test|examples)/**/*.ts'", | ||
"lint-check": "eslint '+(src|test)/**/*.+(ts|tsx)'" | ||
}, | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/descope/descope-js.git" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
"descope", | ||
"authentication" | ||
], | ||
"devDependencies": { | ||
"@rollup/plugin-terser": "^0.4.0", | ||
"@rollup/plugin-typescript": "^11.0.0", | ||
"@types/jest": "^29.0.0", | ||
"@typescript-eslint/parser": "^7.0.0", | ||
"eslint": "8.57.0", | ||
"eslint-config-airbnb-typescript": "18.0.0", | ||
"eslint-config-prettier": "9.1.0", | ||
"eslint-config-standard": "17.1.0", | ||
"eslint-import-resolver-typescript": "3.6.1", | ||
"eslint-plugin-import": "2.29.1", | ||
"eslint-plugin-jest": "28.6.0", | ||
"eslint-plugin-jest-dom": "5.4.0", | ||
"eslint-plugin-jest-formatting": "3.1.0", | ||
"eslint-plugin-n": "17.9.0", | ||
"eslint-plugin-no-only-tests": "3.1.0", | ||
"eslint-plugin-prefer-arrow": "1.2.3", | ||
"eslint-plugin-prettier": "5.1.3", | ||
"eslint-plugin-promise": "6.2.0", | ||
"jest": "^29.0.0", | ||
"jest-environment-jsdom": "^29.0.0", | ||
"prettier": "^3.0.0", | ||
"rimraf": "^5.0.0", | ||
"rollup": "^4.0.0", | ||
"rollup-plugin-auto-external": "^2.0.0", | ||
"rollup-plugin-delete": "^2.0.0", | ||
"rollup-plugin-dts": "^6.0.0", | ||
"ts-jest": "^29.0.0", | ||
"ts-node": "10.9.2", | ||
"typescript": "^5.0.2" | ||
} | ||
} |
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,25 @@ | ||
{ | ||
"name": "escape-markdown", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "packages/libs/escape-markdown/src", | ||
"projectType": "library", | ||
"targets": { | ||
"version": { | ||
"executor": "@jscutlery/semver:version", | ||
"options": { | ||
"trackDeps": true, | ||
"push": false, | ||
"preset": "conventional" | ||
} | ||
}, | ||
"licenseCheck": { | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"commands": [ | ||
"tools/scripts/licenseValidation/thirdPartyLicenseCollector_linux_amd64 -npm-project {projectRoot}" | ||
] | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
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,65 @@ | ||
import typescript from '@rollup/plugin-typescript'; | ||
import fs from 'fs'; | ||
import del from 'rollup-plugin-delete'; | ||
import dts from 'rollup-plugin-dts'; | ||
import terser from '@rollup/plugin-terser'; | ||
|
||
import packageJson from './package.json' assert { type: 'json' }; | ||
|
||
const plugins = [ | ||
typescript({ | ||
tsconfig: './tsconfig.json', | ||
}), | ||
terser(), | ||
]; | ||
const input = './src/index.ts'; | ||
const external = (id) => | ||
!id.startsWith('\0') && !id.startsWith('.') && !id.startsWith('/'); | ||
|
||
export default [ | ||
{ | ||
input, | ||
output: { | ||
file: packageJson.main, | ||
format: 'cjs', | ||
sourcemap: true, | ||
exports: 'named', | ||
interop: 'compat', | ||
inlineDynamicImports: true, | ||
}, | ||
plugins, | ||
external, | ||
}, | ||
{ | ||
input, | ||
output: { | ||
file: packageJson.module, | ||
format: 'esm', | ||
sourcemap: true, | ||
inlineDynamicImports: true, | ||
}, | ||
plugins, | ||
external, | ||
}, | ||
{ | ||
input: './dist/dts/src/index.d.ts', | ||
output: [{ file: packageJson.types, format: 'esm' }], | ||
plugins: [ | ||
dts(), | ||
del({ hook: 'buildEnd', targets: ['./dist/dts', './dist/cjs/dts'] }), | ||
cjsPackage(), | ||
], | ||
}, | ||
]; | ||
|
||
function cjsPackage() { | ||
return { | ||
name: 'cjsPackage', | ||
buildEnd: () => { | ||
fs.writeFileSync( | ||
'./dist/cjs/package.json', | ||
JSON.stringify({ type: 'commonjs' }), | ||
); | ||
}, | ||
}; | ||
} |
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 @@ | ||
const mdChars = ['*', '#', '/', '(', ')', '[', ']', '_', '<', '>', '`']; | ||
|
||
export const escapeMarkdown = (s: string) => { | ||
if (typeof s !== 'string') return s; | ||
const escapedTextRegexp = mdChars.map((char) => `\\${char}`).join('|'); | ||
const regexp = new RegExp(`(${escapedTextRegexp})`, 'g'); | ||
return s.replace(regexp, '\\$1'); | ||
}; |
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 @@ | ||
export * from './escapeMarkdown'; |
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 @@ | ||
import { escapeMarkdown } from '../src'; | ||
|
||
describe('escape markdown', () => { | ||
it('should escape markdown chars', () => { | ||
const source = '*#/()[]_<>`'; | ||
expect(escapeMarkdown(source)).toEqual('\\*\\#\\/\\(\\)\\[\\]\\_\\<\\>\\`'); | ||
}); | ||
|
||
it('ignore non-strings', () => { | ||
const source = 1234; | ||
expect(escapeMarkdown(source as any)).toEqual(source); | ||
}); | ||
}); |
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,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"rootDir": ".", | ||
"target": "es2017", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": false, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"declaration": true, | ||
"declarationDir": "dts", | ||
"noErrorTruncation": true, | ||
"typeRoots": ["./node_modules/@types"] | ||
}, | ||
|
||
"include": ["**/*.ts"], | ||
"exclude": ["node_modules", "build", "dist"] | ||
} |
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
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
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
Oops, something went wrong.