Skip to content

Commit dff1b35

Browse files
committed
Handle options in link
1 parent 45fe3f9 commit dff1b35

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

main.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ export default class SimpleEmbedsPlugin extends Plugin {
110110
});
111111

112112
const disableAutomaticEmbeds = this.settings.disableAutomaticEmbeds;
113-
const replaceWithEmbed = disableAutomaticEmbeds
114-
? a.innerText.includes("|embed")
115-
: !a.innerText.includes("|noembed");
113+
const replaceWithEmbed = this.shouldReplaceWithEmbed(a.innerText);
116114
const fullWidth = a.innerText.includes("|fullwidth");
117115
// Remove any allowed properties:
118116
// |embed, |noembed, |fullwidth
@@ -134,6 +132,13 @@ export default class SimpleEmbedsPlugin extends Plugin {
134132
}
135133
}
136134

135+
shouldReplaceWithEmbed(text: string) {
136+
const disableAutomaticEmbeds = this.settings.disableAutomaticEmbeds;
137+
return disableAutomaticEmbeds
138+
? text.includes("|embed")
139+
: !text.includes("|noembed");
140+
}
141+
137142
createEmbed(
138143
embedSource: EmbedSource,
139144
link: string,

view-plugin.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@ export function buildSimpleEmbedsViewPlugin(plugin: SimpleEmbedsPlugin) {
1414
class EmbedWidget extends WidgetType {
1515
constructor(
1616
readonly link: string,
17+
readonly fullWidth: boolean,
1718
readonly embedSource: EmbedSource,
1819
readonly plugin: SimpleEmbedsPlugin,
1920
) {
2021
super();
2122
}
2223

2324
eq(other: EmbedWidget) {
24-
return other.link === this.link;
25+
return other.link === this.link && other.fullWidth === this.fullWidth;
2526
}
2627

2728
toDOM() {
2829
const embed = this.plugin.createEmbed(
2930
this.embedSource,
3031
this.link,
31-
false,
32+
this.fullWidth,
3233
);
3334
return embed;
3435
}
@@ -96,11 +97,15 @@ export function buildSimpleEmbedsViewPlugin(plugin: SimpleEmbedsPlugin) {
9697
return plugin.settings[source.enabledKey] &&
9798
source.regex.test(line.text);
9899
});
99-
if (embedSource) {
100+
const replaceWithEmbed = plugin.shouldReplaceWithEmbed(mdLink);
101+
const fullWidth = mdLink.includes("|fullwidth");
102+
this.hideOptions(mdLink, startOfLine, builder);
103+
if (embedSource && replaceWithEmbed) {
100104
const link = line.text.match(embedSource.regex).first();
101105
const deco = Decoration.replace({
102106
widget: new EmbedWidget(
103107
link,
108+
fullWidth,
104109
embedSource,
105110
plugin,
106111
),
@@ -121,6 +126,22 @@ export function buildSimpleEmbedsViewPlugin(plugin: SimpleEmbedsPlugin) {
121126

122127
return builder.finish();
123128
}
129+
130+
hideOptions(
131+
text: string,
132+
startOfLine: number,
133+
builder: RangeSetBuilder<Decoration>,
134+
) {
135+
for (let option of ["|noembed", "|embed", "|fullwidth"]) {
136+
if (text.includes(option)) {
137+
const start = text.indexOf(option) + startOfLine;
138+
const end = start + option.length;
139+
140+
const deco = Decoration.replace({});
141+
builder.add(start, end, deco);
142+
}
143+
}
144+
}
124145
},
125146
{
126147
decorations: (v) => v.decorations,

0 commit comments

Comments
 (0)