Skip to content

Commit

Permalink
Add a option to duplicate non-page (e.g. images) resources across lan…
Browse files Browse the repository at this point in the history
…guages

Which will be default behaviour for all other than Goldmark, for which there is a config option:

```
[markup.goldmark]
duplicateResourceFiles = true
```

Fixes #11904
  • Loading branch information
bep committed Jan 22, 2024
1 parent 647097b commit 496b826
Show file tree
Hide file tree
Showing 26 changed files with 480 additions and 228 deletions.
14 changes: 14 additions & 0 deletions common/paths/pathparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ func (p *Path) PathNoIdentifier() string {
return p.base(false, false)
}

// PathRel returns the path relativeto the given owner.
func (p *Path) PathRel(owner *Path) string {
ob := owner.Base()
if !strings.HasSuffix(ob, "/") {
ob += "/"
}
return strings.TrimPrefix(p.Path(), ob)
}

// BaseRel returns the base path relative to the given owner.
func (p *Path) BaseRel(owner *Path) string {
ob := owner.Base()
Expand All @@ -378,6 +387,11 @@ func (p *Path) Base() string {
return p.base(!p.isContentPage(), p.IsBundle())
}

// BaseNoLeadingSlash returns the base path without the leading slash.
func (p *Path) BaseNoLeadingSlash() string {
return p.Base()[1:]
}

func (p *Path) base(preserveExt, isBundle bool) string {
if len(p.identifiers) == 0 {
return p.norm(p.s)
Expand Down
2 changes: 2 additions & 0 deletions common/paths/pathparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func TestParse(t *testing.T) {
func(c *qt.C, p *Path) {
c.Assert(p.Name(), qt.Equals, "b.md")
c.Assert(p.Base(), qt.Equals, "/a/b")
c.Assert(p.BaseNoLeadingSlash(), qt.Equals, "a/b")
c.Assert(p.Section(), qt.Equals, "a")
c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "b")

Expand Down Expand Up @@ -160,6 +161,7 @@ func TestParse(t *testing.T) {
c.Assert(p.NameNoLang(), qt.Equals, "b.a.b.txt")
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"txt", "no"})
c.Assert(p.Base(), qt.Equals, "/a/b.a.b.txt")
c.Assert(p.BaseNoLeadingSlash(), qt.Equals, "a/b.a.b.txt")
c.Assert(p.PathNoLang(), qt.Equals, "/a/b.a.b.txt")
c.Assert(p.Ext(), qt.Equals, "txt")
c.Assert(p.PathNoIdentifier(), qt.Equals, "/a/b.a.b")
Expand Down
8 changes: 7 additions & 1 deletion config/commonConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ var defaultBuild = BuildConfig{

// BuildConfig holds some build related configuration.
type BuildConfig struct {
UseResourceCacheWhen string // never, fallback, always. Default is fallback
// When to use the resource file cache.
// One of never, fallback, always. Default is fallback
UseResourceCacheWhen string

// When enabled, will duplicate bundled resource files across languages that
// doesn't have a translated version.
DuplicateResourceFiles bool

// When enabled, will collect and write a hugo_stats.json with some build
// related aggregated data (e.g. CSS class names).
Expand Down
29 changes: 14 additions & 15 deletions hugolib/content_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ type resourceSource struct {
r resource.Resource
}

func (r resourceSource) clone() *resourceSource {
r.r = nil
return &r
}

func (r *resourceSource) LangIndex() int {
if r.r != nil && r.isPage() {
return r.r.(*pageState).s.languagei
}

return r.fi.Meta().LangIndex
}

func (r *resourceSource) MarkStale() {
resource.MarkStale(r.r)
}
Expand Down Expand Up @@ -151,16 +164,6 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo) error {
commit := tree.Lock(true)
defer commit()

var resources resourceSources
n, ok := tree.GetRaw(key)

if ok {
resources = n.(resourceSources)
} else {
resources = make(resourceSources, len(m.s.h.Sites))
tree.Insert(key, resources)
}

r := func() (hugio.ReadSeekCloser, error) {
return fim.Meta().Open()
}
Expand All @@ -184,11 +187,7 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo) error {
rs = &resourceSource{path: pi, opener: r, fi: fim}
}

i := fim.Meta().LangIndex
if r := resources[i]; r != nil && r.r != nil {
resource.MarkStale(r.r)
}
resources[i] = rs
tree.InsertIntoValuesDimension(key, rs)

return nil
}
Expand Down
Loading

0 comments on commit 496b826

Please sign in to comment.