Skip to content

Commit

Permalink
feat: add typography package
Browse files Browse the repository at this point in the history
  • Loading branch information
kedrzu committed Sep 30, 2024
1 parent b110a0e commit c9620d0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/typography/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
root: true,
extends: [require.resolve('@nzyme/eslint/typescript')],
parserOptions: {
project: `${__dirname}/tsconfig.json`,
},
};
20 changes: 20 additions & 0 deletions packages/typography/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@nzyme/typography",
"version": "1.0.0",
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"sideEffects": false,
"repository": "https://github.com/nzymejs/nzyme.git",
"author": "Michał Kędrzyński <m.kedrzynski@gmail.com>",
"private": true,
"scripts": {
"eslint": "eslint . --fix --cache",
"build": "tsc --build"
},
"dependencies": {
"@nzyme/types": "*",
"@nzyme/utils": "*"
}
}
25 changes: 25 additions & 0 deletions packages/typography/src/fixOrphans.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const spaceAfterShortWordRegex = /([\s(]|^)(\w{1,2})\s/gmu;
const longWordRegex = /\s([^\s]{12,})/gmu;

/**
* Search for any word, that is 3 or less characters long
* Such short words should not end up on the end of the line.
* We add non-breakable space at the end of the word.
* Source: https://boanastudio.com/blog/text-in-responsive-web-design
* @param text Text to fix
*/
export function fixOrphans(text: string) {
// use [^\s] to match anything but whitespace characters
return (
text
.replace(spaceAfterShortWordRegex, (search, whitespace: string, word: string) => {
// replace spaces between short words with a non-breakable space
// do not swap it with any other character!
return whitespace + word + '\xa0';
})
// roll back the operation if following word has 12 or more letters
.replace(longWordRegex, (search, group: string) => {
return ' ' + group;
})
);
}
1 change: 1 addition & 0 deletions packages/typography/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fixOrphans.js';
17 changes: 17 additions & 0 deletions packages/typography/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist",
"tsBuildInfoFile": "./dist/.tsbuildinfo",
"rootDir": "./src"
},
"include": ["src/**/*.ts", "src/**/*.json"],
"references": [
{
"path": "../types/tsconfig.json"
},
{
"path": "../utils/tsconfig.json"
}
]
}

0 comments on commit c9620d0

Please sign in to comment.