Skip to content

Commit

Permalink
refactor(transformer): reuse identifier for the same external demos
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Jan 21, 2021
1 parent d82042c commit 48be11b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</div><DumiDemo4 /><div className="markdown">


</div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react-2'].previewerProps}><DumiDemo5 /></Previewer><div className="markdown"><p>123</p>
<p>000</p></div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react-3'].previewerProps}><DumiDemo6 /></Previewer><div className="markdown"><p><span>456</span></p></div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react-4'].previewerProps}><DumiDemo7 /></Previewer><div className="markdown"><p><em>321</em></p></div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react-5'].previewerProps}><DumiDemo8 /></Previewer><div className="markdown"><p>123<strong>456</strong><em>789</em></p>
</div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react-6'].previewerProps}><DumiDemo9 /></Previewer><div className="markdown">
</div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react-7'].previewerProps}><DumiDemo10 /></Previewer></React.Fragment>
</div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react'].previewerProps}><DumiDemo5 /></Previewer><div className="markdown"><p>123</p>
<p>000</p></div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react'].previewerProps}><DumiDemo6 /></Previewer><div className="markdown"><p><span>456</span></p></div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react'].previewerProps}><DumiDemo7 /></Previewer><div className="markdown"><p><em>321</em></p></div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react'].previewerProps}><DumiDemo8 /></Previewer><div className="markdown"><p>123<strong>456</strong><em>789</em></p>
</div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react'].previewerProps}><DumiDemo9 /></Previewer><div className="markdown">
</div><Previewer {...require('@@/dumi/demos').default['raw-demo-missing-react'].previewerProps}><DumiDemo10 /></Previewer></React.Fragment>
48 changes: 25 additions & 23 deletions packages/preset-dumi/src/transformer/remark/previewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,31 @@ function getPreviewerId(yaml: any, mdAbsPath: string, codeAbsPath: string, compo
// use cache first
id = externalCache.get(codeAbsPath);

const words = (slash(codeAbsPath) as string)
// discard index & suffix like index.tsx
.replace(/(?:\/index)?(\.[\w-]+)?\.\w+$/, '$1')
.split(/\//)
.map(w => w.toLowerCase());
// /path/to/index.tsx -> to || /path/to.tsx -> to
const demoName = words[words.length - 1] || 'demo';
const prefix = words
.slice(0, -1)
.filter(word => !/^(src|_?demos?|_?examples?)$/.test(word))
.pop();

id = `${prefix}-${demoName}`;

// record id count
const currentIdCount = externalIdMap.get(id) || 0;

externalIdMap.set(id, currentIdCount + 1);

// append count suffix
id += currentIdCount ? `-${currentIdCount}` : '';

externalCache.set(codeAbsPath, id);
if (!id) {
const words = (slash(codeAbsPath) as string)
// discard index & suffix like index.tsx
.replace(/(?:\/index)?(\.[\w-]+)?\.\w+$/, '$1')
.split(/\//)
.map(w => w.toLowerCase());
// /path/to/index.tsx -> to || /path/to.tsx -> to
const demoName = words[words.length - 1] || 'demo';
const prefix = words
.slice(0, -1)
.filter(word => !/^(src|_?demos?|_?examples?)$/.test(word))
.pop();

id = `${prefix}-${demoName}`;

// record id count
const currentIdCount = externalIdMap.get(id) || 0;

externalIdMap.set(id, currentIdCount + 1);

// append count suffix
id += currentIdCount ? `-${currentIdCount}` : '';

externalCache.set(codeAbsPath, id);
}
}
}

Expand Down

0 comments on commit 48be11b

Please sign in to comment.