refactor: implement script tag approach for library inclusion
- Add script tags to demo-site HTML files for manual development - Disable CLI inline script injection to prevent duplicate scripts - Add library serving endpoints to servedev command - Update build process to auto-copy library to demo-site - Add CDN URL helpers for future production deployment - Update .gitignore for generated demo-site files Fixes .insertr-gate authentication for manual npm run serve workflow while maintaining clean separation between CLI and manual setups.
This commit is contained in:
@@ -133,32 +133,17 @@ func (i *Injector) addContentAttributes(node *html.Node, contentID string, conte
|
||||
i.addClass(node, "insertr")
|
||||
}
|
||||
|
||||
// InjectEditorAssets adds editor JavaScript and CSS to HTML document
|
||||
// InjectEditorAssets adds editor JavaScript to HTML document
|
||||
func (i *Injector) InjectEditorAssets(doc *html.Node, isDevelopment bool, libraryScript string) {
|
||||
if !isDevelopment {
|
||||
return // Only inject in development mode for now
|
||||
}
|
||||
// TODO: Implement script injection strategy when we have CDN hosting
|
||||
// For now, script injection is disabled since HTML files should include their own script tags
|
||||
// Future options:
|
||||
// 1. Inject CDN script tag: <script src="https://cdn.jsdelivr.net/npm/@insertr/lib@1.0.0/dist/insertr.js"></script>
|
||||
// 2. Inject local script tag for development: <script src="/insertr/insertr.js"></script>
|
||||
// 3. Continue with inline injection for certain use cases
|
||||
|
||||
// Find the head element
|
||||
head := i.findHeadElement(doc)
|
||||
if head == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Add inline script with embedded library
|
||||
// Note: Using html.TextNode for scripts can cause issues with HTML entity encoding
|
||||
// Instead, we'll insert the script tag as raw HTML
|
||||
scriptHTML := fmt.Sprintf(`<script type="text/javascript">
|
||||
%s
|
||||
</script>`, libraryScript)
|
||||
|
||||
// Parse the script HTML and append to head
|
||||
scriptNodes, err := html.ParseFragment(strings.NewReader(scriptHTML), head)
|
||||
if err == nil && len(scriptNodes) > 0 {
|
||||
for _, node := range scriptNodes {
|
||||
head.AppendChild(node)
|
||||
}
|
||||
}
|
||||
// Currently disabled to avoid duplicate scripts
|
||||
return
|
||||
}
|
||||
|
||||
// findHeadElement finds the <head> element in the document
|
||||
|
||||
@@ -2,6 +2,7 @@ package content
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Embedded library assets
|
||||
@@ -24,3 +25,26 @@ func GetLibraryScript(minified bool) string {
|
||||
func GetLibraryVersion() string {
|
||||
return "1.0.0"
|
||||
}
|
||||
|
||||
// GetLibraryURL returns the appropriate library URL for script injection
|
||||
func GetLibraryURL(minified bool, isDevelopment bool) string {
|
||||
if isDevelopment {
|
||||
// Local development URLs - relative to served content
|
||||
if minified {
|
||||
return "/insertr/insertr.min.js"
|
||||
}
|
||||
return "/insertr/insertr.js"
|
||||
}
|
||||
|
||||
// Production URLs - use CDN
|
||||
return GetLibraryCDNURL(minified)
|
||||
}
|
||||
|
||||
// GetLibraryCDNURL returns the CDN URL for production use
|
||||
func GetLibraryCDNURL(minified bool) string {
|
||||
version := GetLibraryVersion()
|
||||
if minified {
|
||||
return fmt.Sprintf("https://cdn.jsdelivr.net/npm/@insertr/lib@%s/dist/insertr.min.js", version)
|
||||
}
|
||||
return fmt.Sprintf("https://cdn.jsdelivr.net/npm/@insertr/lib@%s/dist/insertr.js", version)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user