A Salesforce Lightning Web Component (LWC) that automatically manages content from Salesforce CMS with intelligent image path conversion and margin normalization.
- π― Fetches managed content from Salesforce CMS
- πΌοΈ Automatically converts Salesforce CMS media URLs to community paths
- π Normalizes margins on heading and paragraph elements
- π Handles relative and absolute image paths intelligently
- π± Responsive and lightweight
This component displays CMS-managed content with automatic:
- Image URL Conversion: Converts Salesforce absolute media URLs to community-relative paths for better portability
- Margin Normalization: Removes default margins from
h1-h5andpelements for consistent styling - HTML Entity Decoding: Properly decodes HTML entities in CMS content
- Salesforce DX (SFDX) or Salesforce CLI
- Salesforce org with CMS enabled (Experience Cloud or Digital Experiences)
- Node.js 14+ and npm
- Clone the repository:
git clone https://github.com/Roboghost718/cmsLWComponent.git
cd cmsLWComponent- Authorize your Salesforce org:
sf org login web --set-default
# or (if using older SFDX)
sfdx force:auth:web:login --setdefaultusername- Deploy to your org:
sf project deploy start --metadata force-app
# or (if using older SFDX)
sfdx force:source:pushAdd the component to a Lightning page or Experience Cloud page:
<c-cms-auto-margin-comp content-id="YOUR_CONTENT_KEY"></c-cms-auto-margin-comp>| Parameter | Type | Required | Description |
|---|---|---|---|
contentId |
String | Yes | The content key from Salesforce CMS |
<div>
<c:cmsAutoMarginComp contentId="your-content-key-here" />
</div>- Uses
@wireto fetch content viaManagedContentController.getContent() - Processes HTML and modifies image sources
- Handles error states
getContent(): Fetches a specific content item by content keygetContentList(): Retrieves a list of managed content items- Both methods support pagination, language, and filtering
The component intelligently handles three types of image URLs:
-
Salesforce CMS Absolute URLs
https://*.lightning.force.com/cms/media/ABC123β/sfsites/c/cms/delivery/media/ABC123
-
Relative Paths
image.pngβ/sfsites/c/image.png/images/photo.jpgβ/sfsites/c/images/photo.jpg
-
External/Already Correct URLs (unchanged)
https://example.com/image.png/sfsites/c/assets/image.pngdata:image/png;base64,...
- Source API Version: 65.0+
- Tested with Summer '23 and later releases
- Verify the content is published in CMS
- Check that image media files exist
- Review browser console for detailed logs
- Ensure your community has proper file serving configured
- Confirm the
contentId(content key) is correct - Verify the CMS content is published
- Check user permissions for CMS access
- Review Apex debug logs for API errors
- Uses
with sharingon Apex controller to respect Salesforce sharing rules @AuraEnabled(cacheable=true)for performance- HTML content is parsed in DOM to prevent injection attacks
- Cacheable wire adapters reduce API calls
- Single DOM parse operation
- Minimal JavaScript bundle size
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Please review our CODE_OF_CONDUCT.md before participating.
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Open an issue
- Check existing issues for similar problems
- Include your Salesforce API version and browser details
- Robotghost718 (@Robotghost718)
- Initial release
- Image URL conversion
- Margin normalization
- HTML entity decoding
Built with β€οΈ for the Salesforce community