Skip to content

Commit

Permalink
feat: new card option for disabling indented bullets
Browse files Browse the repository at this point in the history
This is useful for creating cloze deletions on the front and reducing
lots of unnecessary cards created.
  • Loading branch information
aalemayhu committed Nov 12, 2024
1 parent 3666b06 commit 66f3d3c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('SettingsController', () => {
'remove-mp3-links': 'true',
'perserve-newlines': 'true',
'vertex-ai-pdf-questions': 'false',
'disable-indented-bullets': 'false',
});
});

Expand Down
6 changes: 6 additions & 0 deletions src/controllers/SettingsController/supportedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ const supportedOptions = (): CardOption[] => {
'Use Vertex AI API to generate questions from PDFs. This is a paid feature and if enabled will send your notes to Google Cloud.',
false
),
new CardOption(
'disable-indented-bullets',
'Disable Indented Bullets',
'Disable indented bullets from becoming separate cards. This applies to bullet lists.',
false
),
];

return v.filter(Boolean);
Expand Down
45 changes: 31 additions & 14 deletions src/lib/parser/DeckParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { handleNestedBulletPointsInMarkdown } from './handleNestedBulletPointsIn
import { checkFlashcardsLimits } from '../User/checkFlashcardsLimits';
import { extractStyles } from './extractStyles';
import { withFontSize } from './withFontSize';
import { findToggleLists } from './findToggles';

export interface DeckParserInput {
name: string;
Expand Down Expand Up @@ -117,14 +118,6 @@ export class DeckParser {
return dom(selector).toArray();
}

findToggleLists(dom: cheerio.Root): cheerio.Element[] {
const selector =
this.settings.isCherry || this.settings.isAll
? '.toggle'
: '.page-body > ul';
return dom(selector).toArray();
}

removeNestedToggles(input: string) {
return input
.replace(/<details(.*?)>(.*?)<\/details>/g, '')
Expand Down Expand Up @@ -206,9 +199,19 @@ export class DeckParser {
const paragraphs = this.extractCardsFromParagraph(dom);
let cards: Note[] = this.extractCards(dom, toggleList);

const disableIndentedBullets = this.settings.disableIndentedBulletPoints;
// Note: this is a fallback behaviour until we can provide people more flexibility on picking non-toggles
if (cards.length === 0) {
cards.push(...[...this.extractCardsFromLists(dom), ...paragraphs]);
cards.push(
...[
...this.extractCardsFromLists(dom, disableIndentedBullets),
...paragraphs,
]
);
} else if (this.settings.disableIndentedBulletPoints) {
cards.push(
...[...this.extractCardsFromLists(dom, disableIndentedBullets)]
);
}

// Prevent bad cards from leaking out
Expand Down Expand Up @@ -551,7 +554,11 @@ export class DeckParser {
}

private extractToggleLists(dom: cheerio.Root) {
const foundToggleLists = this.findToggleLists(dom);
const foundToggleLists = findToggleLists(dom, {
isCherry: this.settings.isCherry,
isAll: this.settings.isAll,
disableIndentedBulletPoints: this.settings.disableIndentedBulletPoints,
});

return [...foundToggleLists, ...this.findIndentedToggleLists(dom)];
}
Expand Down Expand Up @@ -646,14 +653,24 @@ export class DeckParser {
return paragraphs.map((p) => new Note(dom(p).html() ?? '', ''));
}

private extractCardsFromLists(dom: cheerio.Root) {
private extractCardsFromLists(
dom: cheerio.Root,
disableIndentedBullets: boolean
) {
const cards: Note[] = [];
const lists = [...dom('ul').toArray(), ...dom('ol').toArray()];
const lists = !disableIndentedBullets
? [...dom('ul').toArray(), ...dom('ol').toArray()]
: [...dom('.page-body > .bulleted-list').toArray()];

lists.forEach((list) => {
for (const child of dom(list).find('li')) {
if (!disableIndentedBullets) {
for (const child of dom(list).find('li')) {
this.checkLimits(cards.length, []);
cards.push(new Note(dom(child).html() ?? '', ''));
}
} else {
this.checkLimits(cards.length, []);
cards.push(new Note(dom(child).html() ?? '', ''));
cards.push(new Note(dom(list).html() ?? '', ''));
}
});

Expand Down
3 changes: 3 additions & 0 deletions src/lib/parser/Settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class Settings {
readonly nestedBulletPoints: boolean;

readonly vertexAIPDFQuestions: boolean;
readonly disableIndentedBulletPoints: boolean;

constructor(input: { [key: string]: string }) {
this.deckName = input.deckName;
Expand Down Expand Up @@ -100,6 +101,8 @@ export class Settings {
this.pageEmoji = input['page-emoji'] || 'first_emoji';
this.addNotionLink = input['add-notion-link'] === 'true';
this.vertexAIPDFQuestions = input['vertex-ai-pdf-questions'] === 'true';
this.disableIndentedBulletPoints =
input['disable-indented-bullets'] === 'true';
/* Is this really needed? */
if (this.parentBlockId) {
this.addNotionLink = true;
Expand Down
16 changes: 16 additions & 0 deletions src/lib/parser/findToggles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import cheerio from 'cheerio';

import Settings from './Settings';

export function findToggleLists(
dom: cheerio.Root,
context: Pick<Settings, 'isAll' | 'isCherry' | 'disableIndentedBulletPoints'>
): cheerio.Element[] {
if (context.isCherry || context.isAll) {
return dom('.toggle').toArray();
}
if (!context.disableIndentedBulletPoints) {
return dom('.page-body > ul').toArray();
}
return dom('.page-body > ul:not(.bulleted-list)').toArray();
}

0 comments on commit 66f3d3c

Please sign in to comment.