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:
@@ -80,6 +80,12 @@ func runServedev(cmd *cobra.Command, args []string) {
|
||||
serveEditorAsset(w, r, assetPath)
|
||||
})
|
||||
|
||||
// Handle insertr library files
|
||||
http.HandleFunc("/insertr/", func(w http.ResponseWriter, r *http.Request) {
|
||||
assetPath := strings.TrimPrefix(r.URL.Path, "/insertr/")
|
||||
serveLibraryAsset(w, r, assetPath)
|
||||
})
|
||||
|
||||
// Handle all other requests with our enhanced file server
|
||||
http.Handle("/", fileServer)
|
||||
|
||||
@@ -120,6 +126,24 @@ func serveEditorAsset(w http.ResponseWriter, r *http.Request, assetPath string)
|
||||
http.ServeFile(w, r, assetFile)
|
||||
}
|
||||
|
||||
// serveLibraryAsset serves the insertr library files from embedded assets
|
||||
func serveLibraryAsset(w http.ResponseWriter, r *http.Request, assetPath string) {
|
||||
w.Header().Set("Content-Type", "application/javascript")
|
||||
|
||||
var script string
|
||||
switch assetPath {
|
||||
case "insertr.js":
|
||||
script = content.GetLibraryScript(false)
|
||||
case "insertr.min.js":
|
||||
script = content.GetLibraryScript(true)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte(script))
|
||||
}
|
||||
|
||||
// enhancedFileSystem wraps http.FileSystem to provide enhanced HTML serving
|
||||
type enhancedFileSystem struct {
|
||||
fs http.FileSystem
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ set -e
|
||||
|
||||
echo "🔄 Library changed, rebuilding..."
|
||||
|
||||
# Build the library
|
||||
# Build the library (this will also copy to demo-site)
|
||||
cd ../lib
|
||||
npm run build --silent
|
||||
|
||||
|
||||
Reference in New Issue
Block a user