Skip to content

Commit

Permalink
Add a convert-markdown suggestor.
Browse files Browse the repository at this point in the history
If there are semantic changes to be made by converting markdown, it suggests them.

Part of #670.
  • Loading branch information
jkomoros committed Dec 17, 2023
1 parent 6522ed9 commit 61bd7a7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
import {
convertToQuote
} from './suggestions/convert-to-quote.js';
import { convertMarkdown } from './suggestions/convert-markdown.js';

export const makeReferenceSuggestion = (type : SuggestionType, keyCards: CardID | CardID[], otherCards: CardID | CardID[], referenceType : ReferenceType, reverse = false) : Suggestion => {
//TODO: it's kind of finicky to have to keep track of which ID is which... shouldn't the actions have a sentinel for each that is overriden before being executed?
Expand Down Expand Up @@ -196,6 +197,11 @@ export const SUGGESTORS : {[suggestor in SuggestionType]: Suggestor} = {
generator: convertToQuote,
title: 'Convert to Quote',
color: COLORS.NAVY
},
'convert-markdown': {
generator: convertMarkdown,
title: 'Convert Markdown',
color: COLORS.ROYAL_BLUE
}
};

Expand Down
64 changes: 64 additions & 0 deletions src/suggestions/convert-markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import snarkdown from 'snarkdown';

import {
SuggestorArgs
} from '../suggestions.js';

import {
CardDiff,
Suggestion
} from '../types.js';

import {
TEXT_FIELD_CONFIGURATION,
editableFieldsForCardType
} from '../card_fields.js';

import {
cardDiffHasChanges
} from '../card_diff.js';

import {
TypedObject
} from '../typed_object.js';

import {
htmlIsEquivalent
} from '../util.js';

//There can be missing concept links when concepts were added after a card was added.
export const convertMarkdown = async (args: SuggestorArgs) : Promise<Suggestion[]> => {
//TODO: isn't there a filter that does this?

const {type, card, logger} = args;

const diff : CardDiff = {};

for (const cardField of TypedObject.keys(editableFieldsForCardType(card.card_type))) {
const config = TEXT_FIELD_CONFIGURATION[cardField];
if (!config.html) continue;
const value = card[cardField] as string;
const convertedValue = snarkdown(value);
//The HTML might differ in whitespace.
if (!htmlIsEquivalent(value, convertedValue)) {
logger.info(`Suggesting converting for field ${cardField} ${value} to ${convertedValue}`);
diff[cardField] = convertedValue;
}

}

if (!cardDiffHasChanges(diff)) {
logger.info('No changes proposed by markdown');
return [];
}

return [{
type,
keyCards: [card.id],
supportingCards: [],
action: {
keyCards: diff
}
}];

};
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,8 @@ export type SuggestionType = 'add-see-also'
| 'synthesize-cluster'
| 'remove-priority'
| 'add-concept'
| 'convert-to-quote';
| 'convert-to-quote'
| 'convert-markdown';

export type Suggestion = {
type: SuggestionType,
Expand Down
15 changes: 15 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,21 @@ export const isURL = (str: string) : boolean => {
}
};

export const htmlIsEquivalent = (a : string, b: string) : boolean => {
const doc = getDocument();
if (!doc) throw new Error('no document');
const aDiv = doc.createElement('div');
const bDiv = doc.createElement('div');

aDiv.innerHTML = a;
bDiv.innerHTML = b;

const serializedHTMLA = aDiv.innerHTML.replace(/\s/g, '');
const serializedHTMLB = bDiv.innerHTML.replace(/\s/g, '');

return serializedHTMLA === serializedHTMLB;
};

//Returns a string with the reason that the proposed card type is not legal for
//this card. If the string is '' then it is legal.
export const reasonCardTypeNotLegalForCard = (card : Card, proposedCardType : CardType) => {
Expand Down

0 comments on commit 61bd7a7

Please sign in to comment.