Skip to content

Commit

Permalink
feat: add image support for Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Dec 24, 2024
1 parent 3694d18 commit bbcc226
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 28 deletions.
49 changes: 28 additions & 21 deletions src/lib/parser/DeckParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export class DeckParser {

noLimits: boolean;

workspace: Workspace;
customExporter: CustomExporter;

public get name() {
return this.payload[0].name;
}
Expand All @@ -65,6 +68,11 @@ export class DeckParser {
this.noLimits = input.noLimits;
this.globalTags = null;
this.payload = [];
this.workspace = new Workspace(true, 'fs');
this.customExporter = new CustomExporter(
input.name,
this.workspace.location
);
this.processFirstFile(input.name);
}

Expand All @@ -73,13 +81,16 @@ export class DeckParser {

if (this.settings.nestedBulletPoints && isMarkdownFile(name)) {
const contents = getFileContents(firstFile, false);
this.payload = handleNestedBulletPointsInMarkdown(
this.payload = handleNestedBulletPointsInMarkdown({
name,
contents?.toString(),
this.settings.deckName,
[],
this.settings
);
contents: contents?.toString(),
deckName: this.settings.deckName,
decks: [],
settings: this.settings,
exporter: this.customExporter,
workspace: this.workspace,
files: this.files,

Check failure on line 92 in src/lib/parser/DeckParser.ts

View workflow job for this annotation

GitHub Actions / build (20.18.0)

Type 'import("/home/runner/work/server/server/src/lib/zip/zip").File[]' is not assignable to type 'File[]'.
});
} else if (isHTMLFile(name)) {
const contents = getFileContents(firstFile, true);
this.payload = contents
Expand Down Expand Up @@ -254,13 +265,6 @@ export class DeckParser {
return dom('.page-body > p > del');
}

setupExporter(decks: Deck[], workspace: string) {
for (const d of decks) {
d.style = d.cleanStyle();
}
return new CustomExporter(this.firstDeckName, workspace);
}

// https://stackoverflow.com/questions/6903823/regex-for-youtube-id
_getYouTubeID(input: string) {
return this.ensureNotNull(input, () => {
Expand Down Expand Up @@ -363,7 +367,11 @@ export class DeckParser {
}

build(ws: Workspace) {
const exporter = this.setupExporter(this.payload, ws.location);
if (ws.location !== this.workspace.location) {
console.debug('workspace location changed for build');
console.debug(ws.location);
this.customExporter = new CustomExporter(this.firstDeckName, ws.location);
}

for (const d of this.payload) {
const deck = d;
Expand Down Expand Up @@ -401,7 +409,7 @@ export class DeckParser {
const originalName = dom(elem).attr('src');
if (originalName && isImageFileEmbedable(originalName)) {
const newName = embedFile({
exporter,
exporter: this.customExporter,
files: this.files,
filePath: decodeURIComponent(originalName),
workspace: ws,
Expand Down Expand Up @@ -430,7 +438,7 @@ export class DeckParser {
);
}
const newFileName = embedFile({
exporter,
exporter: this.customExporter,
files: this.files,
filePath: global.decodeURIComponent(audiofile),
workspace: ws,
Expand Down Expand Up @@ -486,13 +494,12 @@ export class DeckParser {
}

this.payload[0].settings = this.settings;
exporter.configure(this.payload);
return exporter.save();
this.customExporter.configure(this.payload);
return this.customExporter.save();
}

tryExperimental(ws: Workspace) {

Check failure on line 501 in src/lib/parser/DeckParser.ts

View workflow job for this annotation

GitHub Actions / build (20.18.0)

'ws' is defined but never used
const fallback = new FallbackParser(this.files);
const exporter = this.setupExporter(this.payload, ws.location);

this.payload = fallback.run(this.settings);
if (
Expand All @@ -504,9 +511,9 @@ export class DeckParser {
}

this.payload[0].settings = this.settings;
exporter.configure(this.payload);
this.customExporter.configure(this.payload);

return exporter.save();
return this.customExporter.save();
}

totalCardCount() {
Expand Down
83 changes: 76 additions & 7 deletions src/lib/parser/handleNestedBulletPointsInMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,41 @@ import { getTitleFromMarkdown } from './getTitleFromMarkdown';
import get16DigitRandomId from '../../shared/helpers/get16DigitRandomId';
import Note from './Note';
import { markdownToHTML } from '../markdown';
import cheerio from 'cheerio';

import CardOption from './Settings';
import { embedFile } from './exporters/embedFile';
import { isImageFileEmbedable } from '../storage/checks';
import CustomExporter from './exporters/CustomExporter';
import Workspace from './WorkSpace';

const BULLET_POINT_REGEX = /^-/;

// Input interface
interface HandleNestedBulletPointsInMarkdownInput {
name: string;
contents: string | undefined;
deckName: string | undefined;
decks: Deck[];
settings: CardOption;
exporter: CustomExporter;
workspace: Workspace;
files: File[];
}

export const handleNestedBulletPointsInMarkdown = (
name: string,
contents: string | undefined,
deckName: string | undefined,
decks: Deck[],
settings: CardOption
input: HandleNestedBulletPointsInMarkdownInput
) => {
const {
name,
contents,
deckName,
decks,
settings,
exporter,
workspace,
files,
} = input;
const deck = new Deck(
deckName ?? getTitleFromMarkdown(contents) ?? name,
[],
Expand All @@ -39,7 +62,30 @@ export const handleNestedBulletPointsInMarkdown = (

const isEnd = lines.length - 1 == lines.indexOf(line);
if (isEnd || (BULLET_POINT_REGEX.exec(line) && isCreating)) {
deck.cards.push(new Note(currentFront, markdownToHTML(currentBack)));
const dom = cheerio.load(currentBack);
const images = dom('img');
const media: string[] = [];

images.each((_i, elem) => {
const src = dom(elem).attr('src');
if (src && isImageFileEmbedable(src)) {
const newName = embedFile({
exporter: exporter, // Adjust as needed
files: files, // Provide the files array
filePath: src,
workspace: workspace, // Provide the workspace
});
if (newName) {
dom(elem).attr('src', newName);
media.push(newName);
}
}
});

currentBack = dom.html();
const note = new Note(currentFront, markdownToHTML(currentBack));
note.media = media;
deck.cards.push(note);
isCreating = false;
currentFront = '';
currentBack = '';
Expand All @@ -55,7 +101,30 @@ export const handleNestedBulletPointsInMarkdown = (
}

if (currentBack !== '' || currentFront !== '') {
deck.cards.push(new Note(currentFront, markdownToHTML(currentBack)));
const dom = cheerio.load(currentBack);
const images = dom('img');
const media: string[] = [];

images.each((_i, elem) => {
const src = dom(elem).attr('src');
if (src && isImageFileEmbedable(src)) {
const newName = embedFile({
exporter,
files: [], // Provide the files array
filePath: src,
workspace,
});
if (newName) {
dom(elem).attr('src', newName);
media.push(newName);
}
}
});

currentBack = dom.html();
const note = new Note(currentFront, markdownToHTML(currentBack));
note.media = media;
deck.cards.push(note);
}

return decks;
Expand Down

0 comments on commit bbcc226

Please sign in to comment.