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
49 changes: 49 additions & 0 deletions __tests__/removeExtraParagraphSpacing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import removeExtraParagraphSpacing from '../android/app/src/main/assets/js/removeExtraParagraphSpacing';

describe('removeExtraParagraphSpacing', () => {
const testCases = [
{
id: '#1',
input: '<p><br>aa<br></p>',
expected: '<p>aa</p>',
},
{
id: '#2',
// Including the \u200b zero-width space mentioned in your comments
input: '<p>bb<br></p>\u200b',
expected: '<p>bb</p>',
},
{
id: '#3',
input: '<p>cccc</p><br><br> <br> <p>ukauka</p>',
expected: '<p>cccc</p><p>ukauka</p>',
},
{
id: '#4',
input: '<p>cccc</p><br> <br><p>ukauka</p>',
expected: '<p>cccc</p><p>ukauka</p>',
},
{
id: '#5',
input: '<p>cccc</p><br> <p>ukauka</p>',
expected: '<p>cccc</p><p>ukauka</p>',
},
{
id: '#6',
input: '<b>aa<br><br> bb<br><br><br>cc</b>',
expected: '<b>aa<br><br> bb<br><br>cc</b>',
},
{
id: '#7',
input: '<p><span style="yes"><br>aa</span></p>',
expected: '<p><span style="yes">aa</span></p>',
},
];

testCases.forEach(({ id, input, expected }) => {
it(`should correctly transform case ${id}`, () => {
const result = removeExtraParagraphSpacing(input);
expect(result).toBe(expected);
});
});
});
17 changes: 1 addition & 16 deletions android/app/src/main/assets/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,22 +771,7 @@ window.addEventListener('load', () => {
}

if (reader.generalSettings.val.removeExtraParagraphSpacing) {
html = html
.replace(/(?:&nbsp;\s*|[\u200b]\s*)+(?=<\/?p[> ])/g, '')
.replace(/<br>\s*<br>\s*(?:<br>\s*)+/g, '<br><br>') //force max 2 consecutive <br>, chaining regex
.replace(
/<br>\s*<br>[^]+/,
_ =>
`${
/\/p>/.test(_)
? _.replace(
/<br>\s*<br>(?:(?=\s*<\/?p[> ])|(?<=<\/?p\b[^>]*><br>\s*<br>))\s*/g,
'',
)
: _
}`,
) //if p found, delete all double br near p
.replace(/<br>(?:(?=\s*<\/?p[> ])|(?<=<\/?p>\s*<br>))\s*/g, '');
html = window.removeExtraParagraphSpacing(html);
}
reader.chapterElement.innerHTML = html;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function removeExtraParagraphSpacing(html: string): string;
28 changes: 28 additions & 0 deletions android/app/src/main/assets/js/removeExtraParagraphSpacing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function removeExtraParagraphSpacing(html) {
return html
.replace(/(?:&nbsp;\s*|[\u200b]\s*)+(?=<\/?p[> ])/g, '')
.replace(/<br>\s*<br>\s*(?:<br>\s*)+/g, '<br><br>') //force max 2 consecutive <br>, chaining regex
.replace(
/<br>\s*<br>[^]+/,
_ =>
`${
/\/p>/.test(_)
? _.replace(
/<br>\s*<br>(?:(?=\s*<\/?p[> ])|(?<=<\/?p\b[^>]*><br>\s*<br>))\s*/g,
'',
)
: _
}`,
) //if p found, delete all double br near p
.replace(/<br>(?:(?=\s*<\/?p[> ])|(?<=<\/?p>\s*<br>))\s*/g, '');
}

// WebView global
if (typeof window !== 'undefined') {
window.removeExtraParagraphSpacing = removeExtraParagraphSpacing;
}

// Node / test
if (typeof module !== 'undefined' && module.exports) {
module.exports = removeExtraParagraphSpacing;
}
18 changes: 18 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ module.exports = {
'!src/**/__tests__/**',
],
},
{
displayName: 'js assets',
preset: 'jest-expo',
roots: ['<rootDir>'],
testMatch: ['__tests__/**/*.test.tsx', '**/__tests__/**/*.test.ts'],
testPathIgnorePatterns: ['/node_modules/', '<rootDir>/src/'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
transform: baseTransform,
transformIgnorePatterns: baseTransformIgnorePatterns,
moduleNameMapper: baseModuleNameMapper,
setupFiles: ['<rootDir>/__mocks__/index.js'],
setupFilesAfterEnv: ['<rootDir>/__tests__/jest.setup.ts'],
collectCoverageFrom: [
'src/**/*.{ts,tsx}',
'!src/database/queries/**/__tests__/**',
'!src/**/__tests__/**',
],
},
],

// Global settings
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@
"engines": {
"node": ">=20"
},
"packageManager": "pnpm@10.27.0"
"packageManager": "pnpm@11.6.0"
}
5 changes: 5 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
allowBuilds:
better-sqlite3: true
esbuild: true
protobufjs: true

nodeLinker: hoisted

onlyBuiltDependencies:
Expand Down
1 change: 1 addition & 0 deletions src/screens/reader/components/WebViewReader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ const WebViewReader: React.FC<WebViewReaderProps> = ({ onPress }) => {
<script src="${assetsUriPrefix}/js/icons.js"></script>
<script src="${assetsUriPrefix}/js/van.js"></script>
<script src="${assetsUriPrefix}/js/text-vibe.js"></script>
<script src="${assetsUriPrefix}/js/removeExtraParagraphSpacing.js"></script>
<script src="${assetsUriPrefix}/js/core.js"></script>
<script src="${assetsUriPrefix}/js/index.js"></script>
<script src="${pluginCustomJS}"></script>
Expand Down
49 changes: 26 additions & 23 deletions src/screens/settings/SettingsReaderScreen/SettingsReaderScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ const SettingsReaderScreen = () => {
--theme-outline: ${theme.outline};
--theme-rippleColor: ${theme.rippleColor};
}

@font-face {
font-family: ${readerSettings.fontFamily};
src: url("file:///android_asset/fonts/${readerSettings.fontFamily
}.ttf");
src: url("file:///android_asset/fonts/${
readerSettings.fontFamily
}.ttf");
}
</style>

Expand Down Expand Up @@ -231,37 +232,39 @@ const SettingsReaderScreen = () => {
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
${webViewCSS}
</head>
<body class="${chapterGeneralSettings.pageReader ? 'page-reader' : ''
}">
<body class="${
chapterGeneralSettings.pageReader ? 'page-reader' : ''
}">
<div id="LNReader-chapter">
${dummyHTML}
</div>
<div id="reader-ui"></div>
</body>
<script>
var initialReaderConfig = ${JSON.stringify({
readerSettings,
chapterGeneralSettings,
novel,
chapter,
nextChapter: chapter,
batteryLevel,
autoSaveInterval: 2222,
DEBUG: __DEV__,
strings: {
finished: `${getString(
'readerScreen.finished',
)}: ${chapter.name.trim()}`,
nextChapter: getString('readerScreen.nextChapter', {
name: chapter.name,
}),
noNextChapter: getString('readerScreen.noNextChapter'),
},
})}
readerSettings,
chapterGeneralSettings,
novel,
chapter,
nextChapter: chapter,
batteryLevel,
autoSaveInterval: 2222,
DEBUG: __DEV__,
strings: {
finished: `${getString(
'readerScreen.finished',
)}: ${chapter.name.trim()}`,
nextChapter: getString('readerScreen.nextChapter', {
name: chapter.name,
}),
noNextChapter: getString('readerScreen.noNextChapter'),
},
})}
</script>
<script src="${assetsUriPrefix}/js/icons.js"></script>
<script src="${assetsUriPrefix}/js/van.js"></script>
<script src="${assetsUriPrefix}/js/text-vibe.js"></script>
<script src="${assetsUriPrefix}/js/removeExtraParagraphSpacing.js"></script>
<script src="${assetsUriPrefix}/js/core.js"></script>
<script src="${assetsUriPrefix}/js/index.js"></script>
<script>
Expand Down
Loading