Skip to content

Commit

Permalink
chore: Escape markdown for dynamic values (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomerlichtash authored Oct 29, 2024
1 parent 49084f8 commit 7c0be21
Show file tree
Hide file tree
Showing 16 changed files with 986 additions and 758 deletions.
90 changes: 90 additions & 0 deletions packages/libs/escape-markdown/.eslintrc.json
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/**/*"]
}
]
}
}
21 changes: 21 additions & 0 deletions packages/libs/escape-markdown/LICENSE
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.
33 changes: 33 additions & 0 deletions packages/libs/escape-markdown/jest.config.cjs
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'],
};
76 changes: 76 additions & 0 deletions packages/libs/escape-markdown/package.json
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"
}
}
25 changes: 25 additions & 0 deletions packages/libs/escape-markdown/project.json
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": []
}
65 changes: 65 additions & 0 deletions packages/libs/escape-markdown/rollup.config.mjs
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' }),
);
},
};
}
8 changes: 8 additions & 0 deletions packages/libs/escape-markdown/src/escapeMarkdown.ts
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');
};
1 change: 1 addition & 0 deletions packages/libs/escape-markdown/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './escapeMarkdown';
13 changes: 13 additions & 0 deletions packages/libs/escape-markdown/test/escapeMarkdown.test.ts
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);
});
});
22 changes: 22 additions & 0 deletions packages/libs/escape-markdown/tsconfig.json
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"]
}
2 changes: 0 additions & 2 deletions packages/sdks/core-js-sdk/src/sdk/flow/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ type JSONSerializable =
| boolean
| null
| Array<JSONSerializable>;

export type FlowInput = Record<string, JSONSerializable>;
3 changes: 2 additions & 1 deletion packages/sdks/web-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"@descope/sdk-helpers": "workspace:*",
"@descope/sdk-mixins": "workspace:*",
"@descope/web-js-sdk": "workspace:*",
"@fingerprintjs/fingerprintjs-pro": "3.9.9"
"@fingerprintjs/fingerprintjs-pro": "3.9.9",
"@descope/escape-markdown": "workspace:*"
},
"overrides": {
"terser": "5.31.1"
Expand Down
5 changes: 5 additions & 0 deletions packages/sdks/web-component/src/lib/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
} from '../constants';
import { AutoFocusOptions, Direction, Locale, SSOQueryParams } from '../types';

const MD_COMPONENTS = ['descope-enriched-text'];

function getUrlParam(paramName: string) {
const urlParams = new URLSearchParams(window.location.search);

Expand Down Expand Up @@ -563,3 +565,6 @@ export const clearPreviousExternalInputs = () => {
.querySelectorAll('[data-hidden-input="true"]')
.forEach((ele) => ele.remove());
};

export const shouldHandleMarkdown = (compName: string) =>
MD_COMPONENTS.includes(compName);
Loading

0 comments on commit 7c0be21

Please sign in to comment.