Skip to content

Commit ff7ac93

Browse files
authored
Addons: add linkpreviews.selector field (#12301)
This field is used to allow users to customize the CSS selector used by addons to grab all the links where they want to apply link previews addons. Required by readthedocs/addons#554
1 parent 966b9ce commit ff7ac93

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

readthedocs/projects/forms.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ class Meta:
687687
"hotkeys_enabled",
688688
"search_enabled",
689689
"linkpreviews_enabled",
690+
"linkpreviews_selector",
690691
"notifications_enabled",
691692
"notifications_show_on_latest",
692693
"notifications_show_on_non_stable",
@@ -701,6 +702,7 @@ class Meta:
701702
"notifications_show_on_non_stable": _("Show a notification on non-stable versions"),
702703
"notifications_show_on_latest": _("Show a notification on latest version"),
703704
"linkpreviews_enabled": _("Enabled"),
705+
"linkpreviews_selector": _("CSS link previews selector"),
704706
"options_root_selector": _("CSS main content selector"),
705707
}
706708

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Generated by Django 5.2.3 on 2025-07-08 09:38
2+
3+
from django.db import migrations
4+
from django.db import models
5+
from django_safemigrate import Safe
6+
7+
8+
class Migration(migrations.Migration):
9+
safe = Safe.before_deploy()
10+
11+
dependencies = [
12+
("projects", "0150_add_ssh_key_with_write_access"),
13+
]
14+
15+
operations = [
16+
migrations.AddField(
17+
model_name="addonsconfig",
18+
name="linkpreviews_selector",
19+
field=models.CharField(
20+
blank=True,
21+
help_text="CSS selector to select links you want enabled for link previews. Leave it blank for auto-detect all links in your main page content.",
22+
max_length=128,
23+
null=True,
24+
),
25+
),
26+
migrations.AddField(
27+
model_name="historicaladdonsconfig",
28+
name="linkpreviews_selector",
29+
field=models.CharField(
30+
blank=True,
31+
help_text="CSS selector to select links you want enabled for link previews. Leave it blank for auto-detect all links in your main page content.",
32+
max_length=128,
33+
null=True,
34+
),
35+
),
36+
]

readthedocs/projects/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ class AddonsConfig(TimeStampedModel):
259259

260260
# Link Previews
261261
linkpreviews_enabled = models.BooleanField(default=False)
262+
linkpreviews_selector = models.CharField(
263+
null=True,
264+
blank=True,
265+
max_length=128,
266+
help_text="CSS selector to select links you want enabled for link previews. Leave it blank for auto-detect all links in your main page content.",
267+
)
262268

263269

264270
class AddonSearchFilter(TimeStampedModel):

readthedocs/proxito/tests/responses/v1.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@
171171
"src": null
172172
},
173173
"linkpreviews": {
174-
"enabled": false
174+
"enabled": false,
175+
"selector": null
175176
}
176177
}
177178
}

readthedocs/proxito/tests/test_hosting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ def test_linkpreviews(self):
742742
)
743743
expected = {
744744
"enabled": True,
745+
"selector": None,
745746
}
746747

747748
assert r.status_code == 200

readthedocs/proxito/views/hosting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ def _v1(self, project, version, build, filename, url, request):
507507
},
508508
"linkpreviews": {
509509
"enabled": project.addons.linkpreviews_enabled,
510+
"selector": project.addons.linkpreviews_selector,
510511
},
511512
"hotkeys": {
512513
"enabled": project.addons.hotkeys_enabled,

0 commit comments

Comments
 (0)