Skip to content

Commit

Permalink
fix: preserve the toggle rendering on the back
Browse files Browse the repository at this point in the history
Fixes a regression due to changes in the Notion style.
  • Loading branch information
aalemayhu committed Nov 2, 2024
1 parent cc3262e commit 3666b06
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 20 deletions.
23 changes: 3 additions & 20 deletions src/lib/parser/DeckParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { isImageFileEmbedable, isMarkdownFile } from '../storage/checks';
import { getFileContents } from './getFileContents';
import { handleNestedBulletPointsInMarkdown } from './handleNestedBulletPointsInMarkdown';
import { checkFlashcardsLimits } from '../User/checkFlashcardsLimits';
import { extractStyles } from './extractStyles';
import { withFontSize } from './withFontSize';

export interface DeckParserInput {
name: string;
Expand Down Expand Up @@ -135,16 +137,6 @@ export class DeckParser {
.replace('<summary class="toggle"></summary>', '');
}

setFontSize(style: string) {
let { fontSize } = this.settings;
if (fontSize && fontSize !== '20px') {
// For backwards compatability, don't touch the font-size if it's 20px
fontSize = fontSize.trim().endsWith('px') ? fontSize : `${fontSize}px`;
style += '\n' + `* { font-size:${fontSize}}`;
}
return style;
}

getLink(pageId: string | undefined, note: Note): string | null {
try {
const page = pageId!.replace(/-/g, '');
Expand Down Expand Up @@ -195,7 +187,7 @@ export class DeckParser {
decks: Deck[]
) {
let dom = this.loadDOM(contents);
const style = this.extractStyle(dom);
const style = withFontSize(extractStyles(dom), this.settings.fontSize);
let image: string | undefined = this.extractCoverImage(dom);

const name = this.extractName(
Expand Down Expand Up @@ -513,15 +505,6 @@ export class DeckParser {
);
}

private extractStyle(dom: cheerio.Root) {
let style = dom('style').html();
if (style) {
style = style.replace(/white-space: pre-wrap;/g, '');
return this.setFontSize(style);
}
return null;
}

private extractCoverImage(dom: cheerio.Root) {
const pageCoverImage = dom('.page-cover-image');
if (pageCoverImage) {
Expand Down
36 changes: 36 additions & 0 deletions src/lib/parser/extractStyles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import cheerio from 'cheerio';
import { extractStyles } from './extractStyles';

describe('extractStyles', () => {
it('should remove list-style-type changes', () => {
const page = cheerio.load(`
<style>.toggle {
\tpadding-inline-start: 0em;
\tlist-style-type: none;
}</style>
`);

const result = extractStyles(page);
expect(result?.trim()).toEqual(
`.toggle {
\tpadding-inline-start: 0em;
\t
}`.trim()
);
});

it('should remove white-space: pre-wrap', () => {
const page = cheerio.load(`
<style>.toggle {
\tpadding-inline-start: 0em;
\twhite-space: pre-wrap;
}</style>`);
const result = extractStyles(page);
expect(result?.trim()).toEqual(
`.toggle {
\tpadding-inline-start: 0em;
\t
}`.trim()
);
});
});
20 changes: 20 additions & 0 deletions src/lib/parser/extractStyles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import cheerio from 'cheerio';

/**
* Extracts the styles from the page and removes formatting issues.
*
* Removing list-style-type changes (makes nested toggles work)
* Removing white-space: pre-wrap (don't remember why)
*
* @param page
*/
export function extractStyles(page: cheerio.Root) {
let style = page('style').html();
if (!style) {
return null;
}

return style
.replace(/white-space: pre-wrap;/g, '')
.replace(/list-style-type: none;/g, '');
}
8 changes: 8 additions & 0 deletions src/lib/parser/withFontSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function withFontSize(style: string | null, fontSize?: string) {
if (style && fontSize && fontSize !== '20px') {
// For backwards compatability, don't touch the font-size if it's 20px
fontSize = fontSize.trim().endsWith('px') ? fontSize : `${fontSize}px`;
style += '\n' + `* { font-size:${fontSize}}`;
}
return style;
}

0 comments on commit 3666b06

Please sign in to comment.