Skip to content

Commit 8305d60

Browse files
committed
update separate-snippets to replace ref docs urls
1 parent b6f9597 commit 8305d60

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

scripts/separate-snippets.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ const RE_END_SNIPPET = /\[END\s+([A-Za-z_]+)\s*\]/;
1818
// TODO: Handle multiline imports?
1919
const RE_REQUIRE = /const {(.+?)} = require\((.+?)\)/;
2020

21+
// Regex for ref docs URLs
22+
// eg. "https://firebase.google.com/docs/reference/js/v8/firebase.User"
23+
const RE_REF_DOCS = /https:\/\/firebase\.google\.com\/docs\/reference\/js\/(.*)/;
24+
25+
// Maps v8 ref docs URLs to their v9 counterpart
26+
const REF_DOCS_MAPPINGS: { [key: string]: string } = {
27+
"v8/firebase.User" : "auth.user"
28+
};
29+
2130
type SnippetsConfig = {
2231
enabled: boolean;
2332
suffix: string;
@@ -30,6 +39,26 @@ function isBlank(line: string) {
3039
return line.trim().length === 0;
3140
}
3241

42+
/**
43+
* Replace all v8 ref doc urls with their v9 counterpart.
44+
*/
45+
function replaceRefDocsUrls(lines: string[]) {
46+
const outputLines = [];
47+
for (const line of lines) {
48+
if (line.match(RE_REF_DOCS)) {
49+
outputLines.push(line.replace(RE_REF_DOCS, (match: string, p1?: string) => {
50+
if (p1) {
51+
return `https://firebase.google.com/docs/reference/js/${REF_DOCS_MAPPINGS[p1]}`;
52+
}
53+
return match;
54+
}));
55+
} else {
56+
outputLines.push(line);
57+
}
58+
}
59+
return outputLines;
60+
}
61+
3362
/**
3463
* Replace all const { foo } = require('bar') with import { foo } from 'bar';
3564
*/
@@ -119,6 +148,7 @@ function processSnippet(
119148
outputLines = addSuffixToSnippetNames(outputLines, snippetSuffix);
120149
outputLines = adjustIndentation(outputLines);
121150
outputLines = removeFirstLineAfterComments(outputLines);
151+
outputLines = replaceRefDocsUrls(outputLines);
122152

123153
// Add a preamble to every snippet
124154
const preambleLines = [

snippets/auth-next/index/auth_current_user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const user = auth.currentUser;
1212

1313
if (user) {
1414
// User is signed in, see docs for a list of available properties
15-
// https://firebase.google.com/docs/reference/js/v8/firebase.User
15+
// https://firebase.google.com/docs/reference/js/auth.user
1616
// ...
1717
} else {
1818
// No user is signed in.

snippets/auth-next/index/auth_state_listener.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const auth = getAuth();
1111
onAuthStateChanged(auth, (user) => {
1212
if (user) {
1313
// User is signed in, see docs for a list of available properties
14-
// https://firebase.google.com/docs/reference/js/v8/firebase.User
14+
// https://firebase.google.com/docs/reference/js/auth.user
1515
const uid = user.uid;
1616
// ...
1717
} else {

0 commit comments

Comments
 (0)