Skip to content

Commit

Permalink
Add markdown support to item description addenda (foundryvtt#14022)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Mar 1, 2024
1 parent 30c0bf8 commit c7514f3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@types/luxon": "^3.4.2",
"@types/node": "20.11.19",
"@types/prompts": "^2.4.9",
"@types/showdown": "^2.0.6",
"@types/sortablejs": "^1.15.8",
"@types/tooltipster": "^0.0.35",
"@types/uuid": "^9.0.8",
Expand Down
16 changes: 12 additions & 4 deletions src/module/item/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,28 @@ class ItemChatData {
data: RawItemChatData;
htmlOptions: EnrichmentOptionsPF2e;

/** A showdown markdown converter */
declare static mdConverter: showdown.Converter;

constructor({ item, data, htmlOptions = {} }: ItemChatDataConstructorOptions) {
this.item = item;
this.data = data;
this.htmlOptions = htmlOptions;
// Workaround vite not resolving import in time
ItemChatData.mdConverter ??= new showdown.Converter();
}

async process(): Promise<RawItemChatData> {
const description = { ...this.data.description, value: await this.#prepareDescription() };
return fu.mergeObject(this.data, { description }, { inplace: false });
}

#sanitizeMarkdown(text: string): string {
const htmlStripped = createHTMLElement("div", { innerHTML: game.i18n.localize(text).trim() }).innerText;
const stringyHTML = ItemChatData.mdConverter.makeHtml(htmlStripped).replace(/<\/?p[^>]*>/g, "");
return TextEditor.truncateHTML(createHTMLElement("div", { innerHTML: stringyHTML })).innerHTML.trim();
}

async #prepareDescription(): Promise<string> {
const { data, item } = this;
const rollOptions = new Set(R.compact([item.actor?.getRollOptions(), item.getRollOptions("item")].flat()));
Expand Down Expand Up @@ -90,10 +101,7 @@ class ItemChatData {
.filter((c) => c.predicate.test(rollOptions))
.map((line) => {
line.title &&= game.i18n.localize(line.title).trim();
const text = game.i18n.localize(line.text).trim();
line.text = text.startsWith("<")
? createHTMLElement("div", { innerHTML: text }).innerText
: text;
line.text = this.#sanitizeMarkdown(line.text);
return line;
}),
};
Expand Down
2 changes: 1 addition & 1 deletion static/templates/items/partials/addendum.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{#if line.divider}}<hr />{{/if}}
<p>
{{#if line.title}}<strong>{{line.title}}</strong>{{/if}}
{{line.text}}
{{{line.text}}}
</p>
{{/each}}
</div>
17 changes: 10 additions & 7 deletions types/foundry/client/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import type GSAP from "gsap";
import type HANDLEBARS from "handlebars";
import { showdown as SHOWDOWN } from "showdownf";

declare global {
const CONST: typeof Constants;

namespace globalThis {
export import Color = Utils.Color;
export import gsap = GSAP;
const Color = Utils.Color;
export import Handlebars = HANDLEBARS;
const gsap = GSAP;
export import showdown = SHOWDOWN;

namespace foundry {
export import CONST = Constants;
export import abstract = Abstract;
export import data = Data;
export import documents = Documents;
export import utils = Utils;
const CONST = Constants;
const abstract = Abstract;
const data = Data;
const documents = Documents;
const utils = Utils;
}
}
}
1 change: 1 addition & 0 deletions types/foundry/common/data/fields.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ export class SchemaField<
override initialize(
value: unknown,
model?: ConstructorOf<abstract.DataModel>,
options?: Record<string, unknown>,
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;

protected override _validateType(
Expand Down
2 changes: 1 addition & 1 deletion types/foundry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"socket.io": "^4.5.1",
"socket.io-client": "^4.5.1",
"tinymce": "^6.1.2",
"typescript": "^4.9.4"
"typescript": "^5.3.3"
},
"author": "the pf2e system developers",
"license": "Apache-2.0",
Expand Down

0 comments on commit c7514f3

Please sign in to comment.