Skip to content

Commit

Permalink
refactor: simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jun 7, 2022
1 parent 9b98920 commit 6f5316a
Showing 1 changed file with 16 additions and 34 deletions.
50 changes: 16 additions & 34 deletions packages/vue-typescript/src/plugins/file-md.ts
Expand Up @@ -10,8 +10,6 @@ export default function (): VueLanguagePlugin {

if (fileName.endsWith('.md')) {

const _content = content;

content = content
// inline code block
.replace(/```[\s\S]*?```/g, match => '```' + ' '.repeat(match.length - 6) + '```')
Expand All @@ -22,21 +20,24 @@ export default function (): VueLanguagePlugin {

const scriptBlockReg = /\<script[\s\S]*?\>([\s\S]*?)\<\/script\>/g;
const styleBlockReg = /\<style[\s\S]*?\>([\s\S]*?)\<\/style\>/g;
const codeGen = new CodeGen();

const scriptBlocks: [number, number][] = [];
const styleBlocks: [number, number][] = [];

for (const match of content.matchAll(scriptBlockReg)) {
if (match.index !== undefined) {
const matchText = match[0];
scriptBlocks.push([match.index, match.index + matchText.length]);
}
}

for (const match of content.matchAll(styleBlockReg)) {
for (const match of [
...content.matchAll(scriptBlockReg),
...content.matchAll(styleBlockReg),
]) {
if (match.index !== undefined) {
const matchText = match[0];
styleBlocks.push([match.index, match.index + matchText.length]);
codeGen.addCode(
content.substring(match.index, match.index + matchText.length),
{
start: match.index,
end: match.index + matchText.length,
},
Mode.Offset,
undefined,
);
codeGen.addText('\n\n');
}
}

Expand All @@ -48,24 +49,6 @@ export default function (): VueLanguagePlugin {
// [foo](http://foo.com)
.replace(/\[[\s\S]*?\]\([\s\S]*?\)/g, match => ' '.repeat(match.length));

const codeGen = new CodeGen();

for (const block of [
...scriptBlocks,
...styleBlocks,
]) {
codeGen.addCode(
_content.substring(block[0], block[1]),
{
start: block[0],
end: block[1],
},
Mode.Offset,
undefined,
);
codeGen.addText('\n\n');
}

codeGen.addText('<template>\n');
codeGen.addCode(
content,
Expand All @@ -76,8 +59,7 @@ export default function (): VueLanguagePlugin {
Mode.Offset,
undefined,
);
codeGen.addText('\n');
codeGen.addText('\n</template>\n');
codeGen.addText('\n</template>');

const sourceMap = new SourceMapBase(codeGen.getMappings());

Expand Down

0 comments on commit 6f5316a

Please sign in to comment.