Skip to content

Commit

Permalink
fix: support pages with newlines in title
Browse files Browse the repository at this point in the history
We are just stripping it away since it causes the export to produce
file names with newlines.

Signed-off-by: Alexander Alemayhu <[email protected]>
  • Loading branch information
aalemayhu committed Dec 26, 2024
1 parent ee0d11c commit fab6e17
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 38 deletions.
56 changes: 56 additions & 0 deletions src/lib/extractDeckName.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { extractName, ExtractNameInput } from "./extractDeckName";

const createInput = (name: string, pageIcon: string | null, decksCount: number, pageEmoji: string) => ({
name,
pageIcon,
decksCount,
settings: { pageEmoji }
});

const createExpectedOutput = (name: string, pageIcon: string | null, pageEmoji: string) => {
if (pageIcon === null || name.includes(pageIcon)) {
return name;
}
return pageEmoji === 'first_emoji' ? `${pageIcon} ${name}` : `${name} ${pageIcon}`;
};

const testCases = [
{
description: 'should return the correct deck name when pageIcon is null',
input: createInput('Default::My Deck', null, 0, 'first_emoji'),
expected: 'Default::My Deck'
},
{
description: 'should return the name with pageIcon prefixed when settings.pageEmoji is "first_emoji"',
input: createInput('My Deck', 'Default', 0, 'first_emoji'),
expected: 'Default My Deck'
},
{
description: 'should return the name with pageIcon suffixed when settings.pageEmoji is not "first_emoji"',
input: createInput('My Deck', 'Default', 0, 'last_emoji'),
expected: 'My Deck Default'
},
{
description: 'should handle names with "::" correctly',
input: createInput('Default::My Deck', 'Default', 0, 'first_emoji'),
expected: 'Default::My Deck'
},
{
description: 'should return the original name if pageIcon is included in the name',
input: createInput('Default::My Deck', 'Default', 1, 'first_emoji'),
expected: 'Default::My Deck'
},
{
description: 'should handle newlines in the deck name correctly',
input: createInput('Default\nMy Deck', 'Default', 0, 'first_emoji'),
expected: 'Default My Deck'
},
];

describe('extractDeckName', () => {
testCases.forEach(({ description, input, expected }) => {
it(description, () => {
expect(extractName(input)).toBe(expected);
});
});
});
39 changes: 39 additions & 0 deletions src/lib/extractDeckName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export interface ExtractNameInput {
name: string;
pageIcon: string | null;
decksCount: number;
settings: {
pageEmoji: string;
};
}

export function extractName(input: ExtractNameInput): string {
const { name, pageIcon: pi, decksCount, settings } = input;
if (!pi) {
return name;
}
if (settings.pageEmoji === 'disable_emoji') {
return name;
}

const cleanName = name.replace(/\n/g, ' ');

if (!cleanName.includes(pi) && decksCount === 0) {
if (!cleanName.includes('::') && !cleanName.startsWith(pi)) {
return settings.pageEmoji === 'first_emoji'
? `${pi} ${cleanName}`
: `${cleanName} ${pi}`;
} else {
const names = cleanName.split(/::/);
const end = names.length - 1;
const last = names[end];
names[end] =
settings.pageEmoji === 'first_emoji'
? `${pi} ${last}`
: `${last} ${pi}`;
return names.join('::');
}
}

return cleanName;
}
2 changes: 1 addition & 1 deletion src/lib/parser/Deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class Deck {
settings: CardOption
) {
this.settings = settings;
this.name = name;
this.name = name.replace(/\n/g, ' ');
this.cards = cards;
this.image = image;
this.style = style;
Expand Down
45 changes: 8 additions & 37 deletions src/lib/parser/DeckParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { withFontSize } from './withFontSize';
import { transformDetailsTagToNotionToggleList } from './transformDetailsTagToNotionToggleList';
import { findNotionToggleLists } from './findNotionToggleLists';
import { NO_PACKAGE_ERROR } from '../error/constants';
import { extractName } from '../extractDeckName';

export interface DeckParserInput {
name: string;
Expand Down Expand Up @@ -203,15 +204,17 @@ export class DeckParser {
const style = withFontSize(extractStyles(dom), this.settings.fontSize);
let image: string | undefined = this.extractCoverImage(dom);

const name = this.extractName(
deckName ||
const name = extractName({
name:
deckName ||
dom('title').text() ||
this.getFirstHeadingText(dom) ||
fileName ||
'Default',
this.extractPageIcon(dom),
decks.length
);
pageIcon: this.extractPageIcon(dom),
decksCount: decks.length,
settings: this.settings,
});

// XXX: review this tag reassignment, does it overwrite?
this.globalTags = this.extractGlobalTags(dom);
Expand Down Expand Up @@ -546,38 +549,6 @@ export class DeckParser {
return pageIcon.html();
}

private extractName(
name: string,
pi: string | null,
decksCount: number
): string {
if (!pi) {
return name;
}
if (this.settings.pageEmoji === 'disable_emoji') {
return name;
}

if (!name.includes(pi) && decksCount === 0) {
if (!name.includes('::') && !name.startsWith(pi)) {
return this.settings.pageEmoji === 'first_emoji'
? `${pi} ${name}`
: `${name} ${pi}`;
} else {
const names = name.split(/::/);
const end = names.length - 1;
const last = names[end];
names[end] =
this.settings.pageEmoji === 'first_emoji'
? `${pi} ${last}`
: `${last} ${pi}`;
return names.join('::');
}
}

return name;
}

private extractToggleLists(dom: cheerio.Root) {
const foundToggleLists = findNotionToggleLists(dom, {
isCherry: this.settings.isCherry,
Expand Down

0 comments on commit fab6e17

Please sign in to comment.