Skip to content

Commit 210c128

Browse files
committed
fix: Support using include tag for snippets
1 parent ed7bb49 commit 210c128

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utilities/nodes.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { LiquidNode } from './types.js'
77

88
const LIQUID_BLOCK_REGEX = /{%-?.*?-?%}/gs
99
const LIQUID_COMMENTS_REGEX = /{%-?\s*comment\s*-?%}[\S\s]*?{%-?\s*endcomment\s*-?%}/gi
10-
const LIQUID_RENDER_REGEX = /\srender\s+'([^']+)'/gs
10+
const LIQUID_RENDER_REGEX = /\s(?:render|include)\s+'([^']+)'/gs
1111
const ASSET_URL_REGEX = /{{\s*'([^']+\.js)'\s*\|\s*asset_url\s*}}/g
1212
const SCRIPT_TAG_REGEX = /<script[^>]*>[\S\s]*?<\/script>/g
1313
const SCRIPT_IMPORT_REGEX = /import\s+["']([^"']+)["']/g
@@ -119,10 +119,27 @@ export async function getCollectionNodes(collectionDir: string): Promise<LiquidN
119119
return Promise.all([...collectionSnippets, ...collectionComponents, ...collectionAssets, ...collectionScripts, ...collectionSetup])
120120
}
121121

122+
function findThemeFolder(file: string, themeDir: string): LiquidNode['themeFolder'] {
123+
const themeFolders = new Set<LiquidNode['themeFolder']>(['layout', 'sections', 'blocks', 'templates'])
124+
let currentDir = path.dirname(file)
125+
126+
while (currentDir !== themeDir && currentDir !== path.dirname(themeDir)) {
127+
const folderName = path.basename(currentDir)
128+
if (themeFolders.has(folderName as LiquidNode['themeFolder'])) {
129+
return folderName as LiquidNode['themeFolder']
130+
}
131+
132+
currentDir = path.dirname(currentDir)
133+
}
134+
135+
// Fallback to the immediate parent directory if no theme folder is found
136+
return path.basename(path.dirname(file)) as LiquidNode['themeFolder']
137+
}
138+
122139
export async function getThemeNodes(themeDir: string): Promise<LiquidNode[]> {
123140
const entryNodes = globSync(path.join(themeDir, '{layout,sections,blocks,templates}', '**/*.liquid'), { absolute: true })
124141
.map(file => {
125-
const parentFolderName = path.basename(path.dirname(file)) as LiquidNode['themeFolder']
142+
const parentFolderName = findThemeFolder(file, themeDir)
126143
return generateLiquidNode(file, 'entry', parentFolderName)
127144
})
128145
const themeSnippets = globSync(path.join(themeDir, 'snippets', '**/*.liquid'), { absolute: true })

test/commands/theme/component/map.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,16 @@ describe('theme component map', () => {
6464
expect(beforeData.files.assets['missing.css']).to.be.undefined
6565
expect(beforeData.files.snippets['missing.liquid']).to.be.undefined
6666
expect(beforeData.files.snippets['missing-subfolder.liquid']).to.be.undefined
67+
expect(beforeData.files.snippets['include-tag.liquid']).to.be.undefined
6768

6869
await runCommand(['theme', 'component', 'map', testThemePath])
6970

7071
// Check that missing entries are present in map
7172
const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8'))
72-
expect(data.files.assets['missing.css']).to.equal('@theme')
73-
expect(data.files.snippets['missing.liquid']).to.equal('@theme')
74-
expect(data.files.snippets['missing-subfolder.liquid']).to.equal('@theme')
73+
expect(data.files.assets['missing.css']).to.equal('@theme') // Should work with assets
74+
expect(data.files.snippets['missing.liquid']).to.equal('@theme') // Should work with render tag
75+
expect(data.files.snippets['missing-subfolder.liquid']).to.equal('@theme') // Should work with subfolders
76+
expect(data.files.snippets['include-tag.liquid']).to.equal('@theme') // Should work with include tag
7577
})
7678

7779
it('adds entries for newly referenced components from current collection', async () => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% include 'include-tag' %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% comment %} This snippet is included using the include tag instead of render {% endcomment %}

0 commit comments

Comments
 (0)