|
| 1 | +# Implementation Summary: DSN Comments and Clipboard Improvements |
| 2 | + |
| 3 | +This document summarizes the implementation of the GitHub issue [#13015](https://github.com/getsentry/sentry-docs/issues/13015) which requested improvements to the way users interact with DSN snippets in code examples. |
| 4 | + |
| 5 | +## Changes Implemented |
| 6 | + |
| 7 | +### 1. Enhanced Clipboard Functionality with Project Names |
| 8 | + |
| 9 | +**Modified Files:** |
| 10 | +- `src/components/codeBlock/index.tsx` |
| 11 | +- `src/components/apiExamples/apiExamples.tsx` |
| 12 | + |
| 13 | +**Changes:** |
| 14 | +- Added `CodeContext` integration to access current project information |
| 15 | +- Modified clipboard "Copied" message to show "Copied for [project name]" instead of just "Copied" |
| 16 | +- Falls back to "Copied" if no project context is available |
| 17 | + |
| 18 | +**Code Changes:** |
| 19 | +```typescript |
| 20 | +// Get the current project name for the copied message |
| 21 | +const getCurrentProjectName = () => { |
| 22 | + if (!codeContext) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + const {codeKeywords, sharedKeywordSelection} = codeContext; |
| 27 | + const [sharedSelection] = sharedKeywordSelection; |
| 28 | + const currentSelectionIdx = sharedSelection['PROJECT'] ?? 0; |
| 29 | + const currentProject = codeKeywords?.PROJECT?.[currentSelectionIdx]; |
| 30 | + |
| 31 | + return currentProject?.title; |
| 32 | +}; |
| 33 | + |
| 34 | +const projectName = getCurrentProjectName(); |
| 35 | +const copiedMessage = projectName ? `Copied for ${projectName}` : 'Copied'; |
| 36 | +``` |
| 37 | + |
| 38 | +### 2. Automatic DSN Comments in Code Examples |
| 39 | + |
| 40 | +**New File Created:** |
| 41 | +- `src/remark-dsn-comments.js` |
| 42 | + |
| 43 | +**Modified Files:** |
| 44 | +- `src/mdx.ts` (added plugin to processing pipeline) |
| 45 | + |
| 46 | +**Functionality:** |
| 47 | +- Automatically adds helpful comments above DSN patterns in code blocks |
| 48 | +- Supports multiple programming languages with appropriate comment syntax: |
| 49 | + - JavaScript/TypeScript: `// Hover over the DSN to see your project, or click it to select a different one` |
| 50 | + - Python/Ruby/Shell: `# Hover over the DSN to see your project, or click it to select a different one` |
| 51 | + - Java/C/C++/etc.: `// Hover over the DSN to see your project, or click it to select a different one` |
| 52 | + - HTML/XML: `<!-- Hover over the DSN to see your project, or click it to select a different one -->` |
| 53 | + - CSS: `/* Hover over the DSN to see your project, or click it to select a different one */` |
| 54 | + - YAML/TOML: `# Hover over the DSN to see your project, or click it to select a different one` |
| 55 | + |
| 56 | +**Processing Logic:** |
| 57 | +- Uses AST (Abstract Syntax Tree) processing via remark plugin |
| 58 | +- Detects `___PROJECT.DSN___` patterns in code blocks |
| 59 | +- Adds language-appropriate comments above DSN lines |
| 60 | +- Prevents duplicate comments if they already exist |
| 61 | +- Skips JSON files (which don't support comments) |
| 62 | + |
| 63 | +## How It Works |
| 64 | + |
| 65 | +### Project Context Integration |
| 66 | +The existing `CodeContext` system already provides: |
| 67 | +- Current selected project information via `sharedKeywordSelection` |
| 68 | +- Project titles formatted as "org-name / project-name" |
| 69 | +- Hover tooltips on DSN values showing project names |
| 70 | + |
| 71 | +### Remark Plugin Processing |
| 72 | +The new `remarkDsnComments` plugin is integrated into the MDX processing pipeline: |
| 73 | +1. Processes all code blocks during build time |
| 74 | +2. Searches for DSN patterns using regex: `/___PROJECT\.DSN___/g` |
| 75 | +3. Determines appropriate comment syntax based on language |
| 76 | +4. Inserts comments above DSN lines |
| 77 | +5. Prevents duplicate comments |
| 78 | + |
| 79 | +### Language Support |
| 80 | +The plugin supports all major programming languages used in Sentry documentation: |
| 81 | +- C-style languages (JavaScript, TypeScript, Java, C++, etc.) |
| 82 | +- Python-style languages (Python, Ruby, Shell, YAML, etc.) |
| 83 | +- Web languages (HTML, CSS, XML) |
| 84 | +- Configuration formats (TOML, YAML) |
| 85 | + |
| 86 | +## User Experience Improvements |
| 87 | + |
| 88 | +### Before |
| 89 | +- Users saw generic "Copied" message when copying code |
| 90 | +- No guidance about DSN hover functionality |
| 91 | +- Users had to discover project selection feature on their own |
| 92 | + |
| 93 | +### After |
| 94 | +- Users see "Copied for [specific-project]" message, confirming which project the code is for |
| 95 | +- Clear instructions appear above DSN in code examples |
| 96 | +- Better discoverability of hover/click functionality for project selection |
| 97 | + |
| 98 | +## Testing |
| 99 | + |
| 100 | +The implementation has been added to the codebase but couldn't be fully tested due to missing production environment variables. However, the code changes are: |
| 101 | + |
| 102 | +1. **Type-safe**: Using existing TypeScript interfaces and patterns |
| 103 | +2. **Backward-compatible**: Falls back gracefully when project context is unavailable |
| 104 | +3. **Performance-conscious**: Uses existing context without additional API calls |
| 105 | +4. **Consistent**: Follows existing code patterns and styling |
| 106 | + |
| 107 | +## Files Modified |
| 108 | + |
| 109 | +### Core Implementation |
| 110 | +- `src/remark-dsn-comments.js` (new) |
| 111 | +- `src/components/codeBlock/index.tsx` |
| 112 | +- `src/components/apiExamples/apiExamples.tsx` |
| 113 | +- `src/mdx.ts` |
| 114 | + |
| 115 | +### No Breaking Changes |
| 116 | +- All changes are additive and backward-compatible |
| 117 | +- Existing functionality remains unchanged |
| 118 | +- New features enhance user experience without disrupting current workflows |
| 119 | + |
| 120 | +## Future Considerations |
| 121 | + |
| 122 | +1. **Testing**: Unit tests could be added for the remark plugin |
| 123 | +2. **Customization**: Comment text could be made configurable if needed |
| 124 | +3. **Internationalization**: Comment text could be localized for different languages |
| 125 | +4. **Analytics**: Could track usage of the enhanced clipboard functionality |
| 126 | + |
| 127 | +The implementation successfully addresses both requirements from the GitHub issue: |
| 128 | +1. ✅ Added helpful comments above DSN in code examples |
| 129 | +2. ✅ Enhanced clipboard messages to include specific project names |
0 commit comments