Skip to content

Commit

Permalink
feat: support extraction for svelte, close #621
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 14, 2021
1 parent 841bb39 commit 57d9d96
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/by-frameworks/svelte/src/components/Raw.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script>
export let name;
let a = 'Hello'
</script>

<main>
<h1>Hello {name}!</h1>
<p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
</main>

<style>
main {
text-align: center;
padding: 1em;
max-width: 240px;
margin: 0 auto;
}
h1 {
color: #ff3e00;
text-transform: uppercase;
font-size: 4em;
font-weight: 100;
}
@media (min-width: 640px) {
main {
max-width: none;
}
}
</style>
41 changes: 41 additions & 0 deletions src/frameworks/svelte.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { TextDocument } from 'vscode'
import { Framework } from './base'
import { LanguageId } from '~/utils'
import { DetectionResult, Config } from '~/core'
import { extractionsParsers, DefaultExtractionRules, DefaultDynamicExtractionsRules } from '~/extraction'
import { shiftDetectionPosition } from '~/extraction/parsers/utils'

class SvelteFramework extends Framework {
id= 'svelte'
Expand Down Expand Up @@ -28,6 +32,43 @@ class SvelteFramework extends Framework {
keypath,
]
}

supportAutoExtraction = ['svelte']

detectHardStrings(doc: TextDocument) {
const text = doc.getText()

const result: DetectionResult[] = []

result.push(
...extractionsParsers.html.detect(
text,
DefaultExtractionRules,
DefaultDynamicExtractionsRules,
Config.extractParserHTMLOptions,
),
)

// <script>
const scriptMatch = text.match(/(<script[^>]*?>)([\s\S*]*?)<\/script>/)
if (scriptMatch && scriptMatch.index != null && scriptMatch.length > 2) {
const index = scriptMatch.index + scriptMatch[1].length
const code = scriptMatch[2]

result.push(
...shiftDetectionPosition(
extractionsParsers.babel.detect(
code,
DefaultExtractionRules,
DefaultDynamicExtractionsRules,
),
index,
),
)
}

return result
}
}

export default SvelteFramework

0 comments on commit 57d9d96

Please sign in to comment.