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
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ jobs:
version: 8
- run: pnpm install
- run: pnpm lint
- run: pnpm format
- run: pnpm test
- run: pnpm build
3 changes: 3 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
- name: Run lint
run: pnpm lint

- name: Run format
run: pnpm format

- name: Run tests
run: pnpm test

Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 80,
"tabWidth": 2
}
6 changes: 5 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import tseslint from "typescript-eslint";
import prettier from "eslint-plugin-prettier";
import prettierConfig from "eslint-config-prettier";

export default tseslint.config(
{
Expand All @@ -14,10 +16,12 @@ export default tseslint.config(
},
plugins: {
"@typescript-eslint": tseslint.plugin,
prettier,
},
rules: {
...tseslint.configs.recommended.rules,

...prettierConfig.rules, // disables conflicting ESLint rules
"prettier/prettier": "error", // enforce Prettier formatting
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/explicit-function-return-type": "off",
},
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"prepublishOnly": "pnpm build",
"lint": "eslint src --ext .ts",
"lint:fix": "eslint src --ext .ts --fix",
"format": "prettier --check \"src/**/*.{ts,js,json,md}\"",
"format:fix": "prettier --write \"src/**/*.{ts,js,json,md}\"",
"test": "jest",
"test:watch": "jest --watchAll"
},
Expand All @@ -36,7 +38,10 @@
"@types/jest": "^30.0.0",
"@types/node": "^24.3.0",
"eslint": "^9.34.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.4",
"jest": "^30.1.1",
"prettier": "^3.6.2",
"ts-jest": "^29.4.1",
"ts-node": "^10.9.2",
"tsup": "^8.5.0",
Expand Down
62 changes: 62 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import preserveFormat from "./utils/preserveFormat";
import preserveFormat from './utils/preserveFormat';

export interface TextifyOptions {
html: string;
Expand All @@ -12,21 +12,21 @@ export function textify({
ignoreTags = [],
}: TextifyOptions): string {
// Ignore rest of the function if it's already empty
if (!html) return "";
if (!html) return '';

if (preserveFormatting) {
// Keep readable formatting
html = preserveFormat({ html, ignoreTags });
} else {
if (ignoreTags.length === 0) {
// Strip all tags
html = html.replace(/<[^>]+>/g, "").trim();
html = html.replace(/<[^>]+>/g, '').trim();
} else {
// Regex to match all tags except the ignored ones
const IG = new Set(ignoreTags.map((t) => t.toLowerCase()));
html = html
.replace(/<\/?([a-z][a-z0-9-]*)\b[^>]*>/gi, (match, tag) =>
IG.has(tag.toLowerCase()) ? match : ""
IG.has(tag.toLowerCase()) ? match : ''
)
.trim();
}
Expand Down
80 changes: 40 additions & 40 deletions src/textify.test.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
import { textify } from "./index";
import { textify } from './index';

describe("textify", () => {
test("returns empty string if html is empty", () => {
expect(textify({ html: "" })).toBe("");
expect(textify({ html: null as unknown as string })).toBe("");
describe('textify', () => {
test('returns empty string if html is empty', () => {
expect(textify({ html: '' })).toBe('');
expect(textify({ html: null as unknown as string })).toBe('');
});

test("strips all tags except ignored ones", () => {
test('strips all tags except ignored ones', () => {
const html =
"<p>Paragraph <b><mark>bold</mark></b><foo /> <i>italic</i><foo/></p>";
'<p>Paragraph <b><mark>bold</mark></b><foo /> <i>italic</i><foo/></p>';
const result = textify({
html,
preserveFormatting: true,
ignoreTags: ["mark", "foo"],
ignoreTags: ['mark', 'foo'],
});
expect(result).toBe("Paragraph **<mark>bold</mark>**<foo />*italic*<foo/>");
expect(result).toBe('Paragraph **<mark>bold</mark>**<foo />*italic*<foo/>');
});

test("handles multiple ignored tags", () => {
const html = "<p>Paragraph <b>bold</b> <i>italic</i> <u>underlined</u></p>";
test('handles multiple ignored tags', () => {
const html = '<p>Paragraph <b>bold</b> <i>italic</i> <u>underlined</u></p>';
const result = textify({
html,
preserveFormatting: false,
ignoreTags: ["b", "u"],
ignoreTags: ['b', 'u'],
});
expect(result).toBe("Paragraph <b>bold</b> italic <u>underlined</u>");
expect(result).toBe('Paragraph <b>bold</b> italic <u>underlined</u>');
});

test("trims whitespace after stripping tags", () => {
const html = " <p>Test</p> ";
test('trims whitespace after stripping tags', () => {
const html = ' <p>Test</p> ';
const result = textify({ html, preserveFormatting: false });
expect(result).toBe("Test");
expect(result).toBe('Test');
});

test("preserveFormat has no effect when they are in ignoreTags", () => {
const html = "<p>Paragraph <b>bold</b> <i>italic</i></p>";
test('preserveFormat has no effect when they are in ignoreTags', () => {
const html = '<p>Paragraph <b>bold</b> <i>italic</i></p>';
const result = textify({
html,
preserveFormatting: true,
ignoreTags: ["b", "i"],
ignoreTags: ['b', 'i'],
});
expect(result).toBe("Paragraph <b>bold</b><i>italic</i>");
expect(result).toBe('Paragraph <b>bold</b><i>italic</i>');
});

test("removes all tags when ignoreTags is empty", () => {
const html = "<div>Hello <span>World</span></div>";
test('removes all tags when ignoreTags is empty', () => {
const html = '<div>Hello <span>World</span></div>';
const result = textify({ html, preserveFormatting: false, ignoreTags: [] });
expect(result).toBe("Hello World");
expect(result).toBe('Hello World');
});

test("case-insensitive matching for ignoreTags", () => {
const html = "<P>Text with <B>bold</B> tag</P>";
test('case-insensitive matching for ignoreTags', () => {
const html = '<P>Text with <B>bold</B> tag</P>';
const result = textify({
html,
preserveFormatting: false,
ignoreTags: ["b"],
ignoreTags: ['b'],
});
expect(result).toBe("Text with <B>bold</B> tag");
expect(result).toBe('Text with <B>bold</B> tag');
});

test("self-closing ignored tags are preserved", () => {
const html = "Line break<br/>Next line";
test('self-closing ignored tags are preserved', () => {
const html = 'Line break<br/>Next line';
const result = textify({
html,
preserveFormatting: false,
ignoreTags: ["br"],
ignoreTags: ['br'],
});
expect(result).toBe("Line break<br/>Next line");
expect(result).toBe('Line break<br/>Next line');
});

test("self-closing non-ignored tags are stripped", () => {
const html = "Line break<br/>Next line";
test('self-closing non-ignored tags are stripped', () => {
const html = 'Line break<br/>Next line';
const result = textify({
html,
preserveFormatting: false,
ignoreTags: [],
});
expect(result).toBe("Line breakNext line");
expect(result).toBe('Line breakNext line');
});

test("ignores invalid or unknown tags if not in ignoreTags", () => {
const html = "Hello <unknown>???</unknown> World";
test('ignores invalid or unknown tags if not in ignoreTags', () => {
const html = 'Hello <unknown>???</unknown> World';
const result = textify({
html,
preserveFormatting: false,
ignoreTags: [],
});
expect(result).toBe("Hello ??? World");
expect(result).toBe('Hello ??? World');
});

test("preserveFormatting=true delegates to preserveFormat", () => {
const html = "<p>Hello <b>world</b></p>";
test('preserveFormatting=true delegates to preserveFormat', () => {
const html = '<p>Hello <b>world</b></p>';
const result = textify({ html, preserveFormatting: true });
// since preserveFormat handles it, just check it returns something non-empty
expect(result).not.toBe("");
expect(result).not.toBe('');
});
});
Loading