Skip to content

Commit

Permalink
fix(pv-stylemark): fix regex extracting css code block
Browse files Browse the repository at this point in the history
the regexes where expecting always an empty space after .css which is only the case when a hidden
attribute is used, it allowed for the info string to come on the next line as the code fence, only
allowed ../ but not ./
  • Loading branch information
mbehzad committed Apr 5, 2024
1 parent 2085297 commit fe3bb8d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/pv-stylemark/tasks/lsg/getLsgData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const { resolveApp, getAppConfig } = require("../../helper/paths");

const getStylesData = stylesMatch => {
const exampleKeys = stylesMatch
.match(/^\s*[a-zA-Z\-\d_]+.css/g)
.map(match => match.replace(/\s/g, "").replace(/.css$/g, ""));
.match(/^ *[\w\-]+\.css/)
.map(match => match.replace(/ /g, "").replace(/\.css$/g, ""));
if (exampleKeys.length === 0) return null;

const styleContent = stylesMatch.replace(/^\s*[a-zA-Z\-\d_]+.css\s+(hidden)?\s+/g, "").trim();
const styleContent = stylesMatch.replace(/^ *[\w\-]+\.css( +hidden)?\s+/g, "").trim();
return {
exampleKey: exampleKeys[0],
styleContent,
Expand Down Expand Up @@ -59,14 +59,14 @@ const getLsgDataForPath = async (path, componentsSrc) => {

const { attributes: frontmatterData, body: fileContentBody } = frontmatter(fileContent);

const stylesRegex = new RegExp(/```\s*([a-zA-Z\-\d_]+\.css (hidden)?)\s*[^```]+```/g);
const stylesRegex = new RegExp(/``` *[\w\-]+\.css( +hidden)? *\n+[^```]+```/g);

const stylesMatches = fileContentBody.match(stylesRegex) || [];

const styles = stylesMatches.map(match => match.replace(/```/g, ""));
const stylesList = styles.map(getStylesData);

const exampleRegex = new RegExp(/```\s*[a-zA-Z\-\d_]+:(\.\.\/)*[a-zA-Z\-\d_/]+\.[a-z]+\s*```/g);
const exampleRegex = new RegExp(/``` *[\w\-]+:(\.?\.\/)*[\w\-/]+\.[a-z]+\s*\n```/g);

const exampleMatches = fileContentBody.match(exampleRegex) || [];
const examples = exampleMatches.map(match => match.replace(/```/g, "").replace(/\s/g, ""));
Expand Down

0 comments on commit fe3bb8d

Please sign in to comment.