Skip to content

Commit 57d9d96

Browse files
committed
feat: support extraction for svelte, close #621
1 parent 841bb39 commit 57d9d96

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script>
2+
export let name;
3+
4+
let a = 'Hello'
5+
</script>
6+
7+
<main>
8+
<h1>Hello {name}!</h1>
9+
<p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
10+
</main>
11+
12+
<style>
13+
main {
14+
text-align: center;
15+
padding: 1em;
16+
max-width: 240px;
17+
margin: 0 auto;
18+
}
19+
h1 {
20+
color: #ff3e00;
21+
text-transform: uppercase;
22+
font-size: 4em;
23+
font-weight: 100;
24+
}
25+
@media (min-width: 640px) {
26+
main {
27+
max-width: none;
28+
}
29+
}
30+
</style>

Diff for: src/frameworks/svelte.ts

+41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { TextDocument } from 'vscode'
12
import { Framework } from './base'
23
import { LanguageId } from '~/utils'
4+
import { DetectionResult, Config } from '~/core'
5+
import { extractionsParsers, DefaultExtractionRules, DefaultDynamicExtractionsRules } from '~/extraction'
6+
import { shiftDetectionPosition } from '~/extraction/parsers/utils'
37

48
class SvelteFramework extends Framework {
59
id= 'svelte'
@@ -28,6 +32,43 @@ class SvelteFramework extends Framework {
2832
keypath,
2933
]
3034
}
35+
36+
supportAutoExtraction = ['svelte']
37+
38+
detectHardStrings(doc: TextDocument) {
39+
const text = doc.getText()
40+
41+
const result: DetectionResult[] = []
42+
43+
result.push(
44+
...extractionsParsers.html.detect(
45+
text,
46+
DefaultExtractionRules,
47+
DefaultDynamicExtractionsRules,
48+
Config.extractParserHTMLOptions,
49+
),
50+
)
51+
52+
// <script>
53+
const scriptMatch = text.match(/(<script[^>]*?>)([\s\S*]*?)<\/script>/)
54+
if (scriptMatch && scriptMatch.index != null && scriptMatch.length > 2) {
55+
const index = scriptMatch.index + scriptMatch[1].length
56+
const code = scriptMatch[2]
57+
58+
result.push(
59+
...shiftDetectionPosition(
60+
extractionsParsers.babel.detect(
61+
code,
62+
DefaultExtractionRules,
63+
DefaultDynamicExtractionsRules,
64+
),
65+
index,
66+
),
67+
)
68+
}
69+
70+
return result
71+
}
3172
}
3273

3374
export default SvelteFramework

0 commit comments

Comments
 (0)