Skip to content

Commit e72b8b4

Browse files
#1143: Add func. to specify if pattern has own dir
1 parent e39a881 commit e72b8b4

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed

packages/core/src/lib/markdown_parser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ const markdown_parser = function() {
1313
let returnObject = {};
1414

1515
try {
16-
//for each block process the yaml frontmatter and markdown
17-
const frontmatterRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
16+
// for each block process the yaml frontmatter and markdown
17+
// even if the pattern only has pattern data without further documentation
18+
const frontmatterRE = /---\r?\n{1}([\s\S]*)---([\s\S]*)+/gm;
1819
const chunks = frontmatterRE.exec(block);
1920

2021
if (chunks) {

packages/core/src/lib/object_factory.js

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@ const prefixMatcher = /^_?(\d+-)?/;
1616
* to get more details about the behavior of the folder structure
1717
* https://github.com/pattern-lab/patternlab-node/pull/992
1818
* https://github.com/pattern-lab/patternlab-node/pull/1016
19+
* https://github.com/pattern-lab/patternlab-node/pull/1143
1920
*
20-
* @constructor
21+
* @param {String} relPath relative directory
22+
* @param {Object} jsonFileData The JSON used to render values in the pattern.
23+
* @param {Patternlab} patternlab The actual patternlab instance
24+
* @param {Boolean} isUnsubRun specifies if the pattern needs to be unsubbed from its folder
2125
*/
22-
const Pattern = function(relPath, data, patternlab) {
26+
const Pattern = function(relPath, jsonFileData, patternlab, isUnsubRun) {
2327
this.relPath = path.normalize(relPath); // '00-atoms/00-global/00-colors.mustache'
2428

2529
/**
2630
* We expect relPath to be the path of the pattern template, relative to the
2731
* root of the pattern tree. Parse out the path parts and save the useful ones.
28-
* @param {relPath} relative directory
29-
* @param {data} The JSON used to render values in the pattern.
30-
* @param {patternlab} rendered html files for the pattern
3132
*/
3233
const pathObj = path.parse(this.relPath);
3334

34-
const info = this.getPatternInfo(pathObj, patternlab);
35+
const info = this.getPatternInfo(pathObj, patternlab, isUnsubRun);
3536

3637
this.fileName = pathObj.name; // '00-colors'
3738
this.subdir = pathObj.dir; // '00-atoms/00-global'
@@ -41,8 +42,8 @@ const Pattern = function(relPath, data, patternlab) {
4142
if (info.patternHasOwnDir) {
4243
// Since there is still the requirement of having the numbers provided for sorting
4344
// this will be required to keep the folder prefix and the variant name
44-
// /00-atoms/00-global/00-colors/colors~varian.hbs
45-
// -> 00-atoms-00-global-00-colors
45+
// /00-atoms/00-global/00-colors/colors~variant.hbs
46+
// -> 00-atoms-00-global-00-colors-variant
4647
this.name = `${info.shortNotation}-${path.parse(pathObj.dir).base}${
4748
this.fileName.indexOf('~') !== -1 ? '-' + this.fileName.split('~')[1] : ''
4849
}`;
@@ -52,7 +53,7 @@ const Pattern = function(relPath, data, patternlab) {
5253
}
5354

5455
// the JSON used to render values in the pattern
55-
this.jsonFileData = data || {};
56+
this.jsonFileData = jsonFileData || {};
5657

5758
// strip leading "00-" from the file name and flip tildes to dashes
5859
this.patternBaseName = this.fileName
@@ -188,8 +189,10 @@ Pattern.prototype = {
188189
return this.name + path.sep + this.name + suffix + '.html';
189190
},
190191

191-
// the finders all delegate to the PatternEngine, which also encapsulates all
192-
// appropriate regexes
192+
/**
193+
* The finders all delegate to the PatternEngine, which also
194+
* encapsulates all appropriate regexes
195+
*/
193196
findPartials: function() {
194197
return this.engine.findPartials(this);
195198
},
@@ -210,9 +213,15 @@ Pattern.prototype = {
210213
return this.engine.findPartial(partialString);
211214
},
212215

213-
getDirLevel: function(level, pathInfo) {
216+
/**
217+
* Get a directory on a specific level of the pattern path
218+
*
219+
* @param {Number} level Level of folder to get
220+
* @param {Object} pInfo general information about the pattern
221+
*/
222+
getDirLevel: function(level, pInfo) {
214223
const items = this.subdir.split(path.sep);
215-
pathInfo.patternHasOwnDir && items.pop();
224+
pInfo && pInfo.patternHasOwnDir && items.pop();
216225

217226
if (items[level]) {
218227
return items[level];
@@ -221,32 +230,54 @@ Pattern.prototype = {
221230
} else {
222231
// Im Not quite shure about that but its better than empty node
223232
// TODO: verify
224-
return pathInfo.patternlab &&
225-
pathInfo.patternlab.config.patternTranslations &&
226-
pathInfo.patternlab.config.patternTranslations['root-name']
227-
? _.kebabCase(
228-
pathInfo.patternlab.config.patternTranslations['root-name']
229-
)
233+
return pInfo.patternlab &&
234+
pInfo.patternlab.config.patternTranslations &&
235+
pInfo.patternlab.config.patternTranslations['root-name']
236+
? _.kebabCase(pInfo.patternlab.config.patternTranslations['root-name'])
230237
: 'root';
231238
}
232239
},
233240

241+
/**
242+
* Reset the information that the pattern has it's own directory,
243+
* so that this pattern will not be handled as flat pattern if it
244+
* is located on a top level folder.
245+
*
246+
* @param {Patternlab} patternlab Current patternlab instance
247+
*/
248+
resetSubbing: function(patternlab) {
249+
const p = new Pattern(this.relPath, this.jsonFileData, patternlab, true);
250+
// Only reset the specific fields, not everything
251+
Object.assign(this, {
252+
patternLink: p.patternLink,
253+
patternGroup: p.patternGroup,
254+
patternType: p.patternType,
255+
patternSubGroup: p.patternSubGroup,
256+
patternSubType: p.patternSubType,
257+
isFlatPattern: p.isFlatPattern,
258+
flatPatternPath: p.flatPatternPath,
259+
patternPartial: p.patternPartial,
260+
verbosePartial: p.verbosePartial,
261+
});
262+
},
263+
234264
/**
235265
* Info contains information about pattern structure if it is a nested pattern
236266
* or if it just a subbed folder structure. Its just used for internal purposes.
237267
* Remember every pattern infomarion based on "this.*" will be used by other functions
238268
*
239269
* @param pathObj path.parse() object containing usefull path information
240270
*/
241-
getPatternInfo: (pathObj, patternlab) => {
271+
getPatternInfo: (pathObj, patternlab, isUnsubRun) => {
242272
const info = {
243273
// 00-colors(.mustache) is subbed in 00-atoms-/00-global/00-colors
244274
patternlab: patternlab,
245-
patternHasOwnDir:
246-
path.basename(pathObj.dir).replace(prefixMatcher, '') ===
247-
pathObj.name.replace(prefixMatcher, '') ||
248-
path.basename(pathObj.dir).replace(prefixMatcher, '') ===
249-
pathObj.name.split('~')[0].replace(prefixMatcher, ''),
275+
patternHasOwnDir: !isUnsubRun
276+
? path.basename(pathObj.dir).replace(prefixMatcher, '') ===
277+
pathObj.name.replace(prefixMatcher, '') ||
278+
path.basename(pathObj.dir).replace(prefixMatcher, '') ===
279+
pathObj.name.split('~')[0].replace(prefixMatcher, '')
280+
: false,
250281
};
251282

252283
info.dir = info.patternHasOwnDir ? pathObj.dir.split(path.sep).pop() : '';

packages/core/src/lib/readDocumentation.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ module.exports = function(pattern, patternlab) {
5454
if (markdownObject.links) {
5555
pattern.links = markdownObject.links;
5656
}
57+
58+
if (markdownObject.hasOwnProperty('subbed') && !markdownObject.subbed) {
59+
// Reset to pattern without own pattern-directory
60+
pattern.resetSubbing(patternlab);
61+
}
5762
} else {
5863
logger.warning(`error processing markdown for ${pattern.patternPartial}`);
5964
}

0 commit comments

Comments
 (0)