Skip to content

Allow <a name=""></a> elements in the markdown. #8796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/lib/shared/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ class _RelativeUrlRewriter extends html_parsing.TreeVisitor {
super.visitElement(element);

// check current element
if (element.localName == 'a') {
if (element.localName == 'a' &&
element.attributes.length == 1 &&
element.attributes.containsKey('name') &&
element.attributes['name']!.isNotEmpty &&
element.nodes.isEmpty) {
// allow name anchor without any other attribute or content
} else if (element.localName == 'a') {
_updateUrlAttributes(element, 'href');
} else if (element.localName == 'img') {
_updateUrlAttributes(element, 'src', raw: true);
Expand Down
10 changes: 10 additions & 0 deletions app/test/shared/markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ void main() {
'</ul>\n',
);
});

test('named anchor', () {
expect(
markdownToHtml('<a name="abc"></a>'), '<p><a name="abc"></a></p>\n');
});

test('named anchor with other content', () {
expect(markdownToHtml('<a name="abc" other="1"></a>'), '<p></p>\n');
expect(markdownToHtml('<a name="abc">x</a>'), '<p>x</p>\n');
});
});

group('Valid custom base URL', () {
Expand Down