Skip to content

Remove the click-to-copy data attribute #578

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 3 commits into from
Jun 4, 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
3 changes: 1 addition & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html/wp-content/plugins/remote-data-blocks": "${workspaceFolder}/",
"/var/www/html/wp-content/plugins/example": "${workspaceFolder}/example"
"/var/www/html/wp-content/plugins/remote-data-blocks": "${workspaceFolder}/"
}
},
{
Expand Down
17 changes: 14 additions & 3 deletions example/blocks/github-markdown-block/inc/markdown-links.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function update_markdown_links( string $html, string $current_file_path = '' ):
$dom = new DOMDocument();

// Convert HTML to UTF-8 using htmlspecialchars instead of mb_convert_encoding
$html = '<?xml encoding="UTF-8">' . $html;
$html = '<?xml encoding="UTF-8"?>' . $html;

// Suppress errors due to malformed HTML
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
Expand Down Expand Up @@ -53,8 +53,19 @@ function update_markdown_links( string $html, string $current_file_path = '' ):
}
}

// Save and return the updated HTML
return $dom->saveHTML();
// Remove the data attributes that GitHub uses for click-to-copy functionality.
// The DOM parser is unable to keep them encoded correctly.
$click_to_copy_attribute = 'data-snippet-clipboard-copy-content';
$nodes = $xpath->query( sprintf( '//*[@%s]', $click_to_copy_attribute ) );
foreach ( $nodes as $node ) {
if ( ! $node instanceof DOMElement ) {
continue;
}
$node->removeAttribute( $click_to_copy_attribute );
}

// Save and return the updated HTML without the XML declaration.
return preg_replace( '/^<\?xml[^>]+\?>/', '', $dom->saveHTML() );
}


Expand Down
Loading