|
| 1 | +# CMS Auto Margin Component |
| 2 | + |
| 3 | +A Salesforce Lightning Web Component (LWC) that automatically manages content from Salesforce CMS with intelligent image path conversion and margin normalization. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🎯 Fetches managed content from Salesforce CMS |
| 8 | +- 🖼️ Automatically converts Salesforce CMS media URLs to community paths |
| 9 | +- 📐 Normalizes margins on heading and paragraph elements |
| 10 | +- 🔗 Handles relative and absolute image paths intelligently |
| 11 | +- 📱 Responsive and lightweight |
| 12 | + |
| 13 | +## Component Overview |
| 14 | + |
| 15 | +This component displays CMS-managed content with automatic: |
| 16 | + |
| 17 | +1. **Image URL Conversion**: Converts Salesforce absolute media URLs to community-relative paths for better portability |
| 18 | +2. **Margin Normalization**: Removes default margins from `h1-h5` and `p` elements for consistent styling |
| 19 | +3. **HTML Entity Decoding**: Properly decodes HTML entities in CMS content |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +### Prerequisites |
| 24 | +- Salesforce DX (SFDX) or Salesforce CLI |
| 25 | +- Salesforce org with CMS enabled (Experience Cloud or Digital Experiences) |
| 26 | +- Node.js 14+ and npm |
| 27 | + |
| 28 | +### Setup |
| 29 | + |
| 30 | +1. Clone the repository: |
| 31 | +```bash |
| 32 | +git clone https://github.com/Roboghost718/cmsLWComponent.git |
| 33 | +cd cmsLWComponent |
| 34 | +``` |
| 35 | + |
| 36 | +2. Authorize your Salesforce org: |
| 37 | +```bash |
| 38 | +sf org login web --set-default |
| 39 | +# or (if using older SFDX) |
| 40 | +sfdx force:auth:web:login --setdefaultusername |
| 41 | +``` |
| 42 | + |
| 43 | +3. Deploy to your org: |
| 44 | +```bash |
| 45 | +sf project deploy start --metadata force-app |
| 46 | +# or (if using older SFDX) |
| 47 | +sfdx force:source:push |
| 48 | +``` |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +### Basic Implementation |
| 53 | + |
| 54 | +Add the component to a Lightning page or Experience Cloud page: |
| 55 | + |
| 56 | +```html |
| 57 | +<c-cms-auto-margin-comp content-id="YOUR_CONTENT_KEY"></c-cms-auto-margin-comp> |
| 58 | +``` |
| 59 | + |
| 60 | +### Input Parameters |
| 61 | + |
| 62 | +| Parameter | Type | Required | Description | |
| 63 | +|-----------|------|----------|-------------| |
| 64 | +| `contentId` | String | Yes | The content key from Salesforce CMS | |
| 65 | + |
| 66 | +### Example in Aura Component |
| 67 | + |
| 68 | +```html |
| 69 | +<div> |
| 70 | + <c:cmsAutoMarginComp contentId="your-content-key-here" /> |
| 71 | +</div> |
| 72 | +``` |
| 73 | + |
| 74 | +## Component Architecture |
| 75 | + |
| 76 | +### JavaScript (`cmsAutoMarginComp.js`) |
| 77 | +- Uses `@wire` to fetch content via `ManagedContentController.getContent()` |
| 78 | +- Processes HTML and modifies image sources |
| 79 | +- Handles error states |
| 80 | + |
| 81 | +### Apex Controller (`ManagedContentController.cls`) |
| 82 | +- `getContent()`: Fetches a specific content item by content key |
| 83 | +- `getContentList()`: Retrieves a list of managed content items |
| 84 | +- Both methods support pagination, language, and filtering |
| 85 | + |
| 86 | +## Image URL Conversion Logic |
| 87 | + |
| 88 | +The component intelligently handles three types of image URLs: |
| 89 | + |
| 90 | +1. **Salesforce CMS Absolute URLs** |
| 91 | + - `https://*.lightning.force.com/cms/media/ABC123` → `/sfsites/c/cms/delivery/media/ABC123` |
| 92 | + |
| 93 | +2. **Relative Paths** |
| 94 | + - `image.png` → `/sfsites/c/image.png` |
| 95 | + - `/images/photo.jpg` → `/sfsites/c/images/photo.jpg` |
| 96 | + |
| 97 | +3. **External/Already Correct URLs** (unchanged) |
| 98 | + - `https://example.com/image.png` |
| 99 | + - `/sfsites/c/assets/image.png` |
| 100 | + - `data:image/png;base64,...` |
| 101 | + |
| 102 | +## Supported Salesforce API Versions |
| 103 | + |
| 104 | +- Source API Version: 65.0+ |
| 105 | +- Tested with Summer '23 and later releases |
| 106 | + |
| 107 | +## Troubleshooting |
| 108 | + |
| 109 | +### Images not loading? |
| 110 | +- Verify the content is published in CMS |
| 111 | +- Check that image media files exist |
| 112 | +- Review browser console for detailed logs |
| 113 | +- Ensure your community has proper file serving configured |
| 114 | + |
| 115 | +### Content not appearing? |
| 116 | +- Confirm the `contentId` (content key) is correct |
| 117 | +- Verify the CMS content is published |
| 118 | +- Check user permissions for CMS access |
| 119 | +- Review Apex debug logs for API errors |
| 120 | + |
| 121 | +## Architecture & Design |
| 122 | + |
| 123 | +### Security |
| 124 | +- Uses `with sharing` on Apex controller to respect Salesforce sharing rules |
| 125 | +- `@AuraEnabled(cacheable=true)` for performance |
| 126 | +- HTML content is parsed in DOM to prevent injection attacks |
| 127 | + |
| 128 | +### Performance |
| 129 | +- Cacheable wire adapters reduce API calls |
| 130 | +- Single DOM parse operation |
| 131 | +- Minimal JavaScript bundle size |
| 132 | + |
| 133 | +## Contributing |
| 134 | + |
| 135 | +We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines. |
| 136 | + |
| 137 | +## Code of Conduct |
| 138 | + |
| 139 | +Please review our [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) before participating. |
| 140 | + |
| 141 | +## License |
| 142 | + |
| 143 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 144 | + |
| 145 | +## Support |
| 146 | + |
| 147 | +For issues and questions: |
| 148 | +- Open an [issue](https://github.com/yourusername/cmsLWComponent/issues) |
| 149 | +- Check existing issues for similar problems |
| 150 | +- Include your Salesforce API version and browser details |
| 151 | + |
| 152 | +## Authors |
| 153 | + |
| 154 | +- Robotghost718 (@Robotghost718) |
| 155 | + |
| 156 | +## Changelog |
| 157 | + |
| 158 | +### Version 1.0.0 |
| 159 | +- Initial release |
| 160 | +- Image URL conversion |
| 161 | +- Margin normalization |
| 162 | +- HTML entity decoding |
| 163 | + |
| 164 | +--- |
| 165 | + |
| 166 | +**Built with ❤️ for the Salesforce community** |
0 commit comments