File size: 3,007 Bytes
f91a684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { MongoClient } from 'mongodb';

async function main() {
  const uri = "mongodb+srv://healthbuddy:healthbuddy123@healthbuddy.sdlagac.mongodb.net/";
  const client = new MongoClient(uri);
  try {
    await client.connect();
    const db = client.db('aptitude');
    const coll = db.collection('quantitative_aptitude');
    const doc = await coll.findOne({ chapter_no: 1 });
    
    let text = doc.content;
    const match = text.match(/(?:^|\n)Q\d+\.\s+/);
    if (match && match.index !== undefined) {
      text = text.slice(0, match.index).trim();
    }
    
    // Markdownify
    let md = text;
    
    // 1. Main headings (e.g., "1.  CONCEPT", "2.  FORMULAS")
    md = md.replace(/^(\d+)\.\s+([A-Z0-9\s&]+)$/gm, '## $1. $2');
    
    // 2. Convert Tab-separated lines into Markdown Tables
    // A line with tabs should be part of a table.
    const lines = md.split('\n');
    let inTable = false;
    let outLines = [];
    
    for (let i = 0; i < lines.length; i++) {
      let line = lines[i];
      
      // If line has a tab, treat it as a table row
      if (line.includes('\t')) {
        if (!inTable) {
          inTable = true;
          // Format header row
          let cells = line.split('\t').map(c => c.trim());
          outLines.push('| ' + cells.join(' | ') + ' |');
          outLines.push('| ' + cells.map(() => '---').join(' | ') + ' |');
        } else {
          // Format data row
          let cells = line.split('\t').map(c => c.trim());
          // Sometimes trick lines start with T1\t... we should treat them as headers if they are tricks?
          // Wait, tricks are formatted as "T1 \t Trick Name \n description". We'll handle them separately if needed.
          outLines.push('| ' + cells.join(' | ') + ' |');
        }
      } else {
        if (inTable) {
          inTable = false;
          outLines.push(''); // spacing
        }
        
        // 3. Subheadings (short lines without punctuation at the end, not empty)
        // Heuristic: line length < 60, no period/comma/colon at the end, doesn't start with '#' or '|'
        if (line.length > 0 && line.length < 60 && !/[.,:;]$/.test(line.trim()) && !line.startsWith('#') && !line.startsWith('|')) {
            // Is it just regular text? Let's see...
            // We'll mark it bold or H3 if it's very distinct.
            // Let's specifically target "Method X:", "Type X", "Core Definition", etc.
            if (/^(Type \d+|Method \d+|T\d+|Core Definition|Key Relationships|Basic Formulas|Percentage Change|Reverse Percentage|Successive Percentage Change|Population & Depreciation|Comparison Between Two Quantities|Expenditure & Consumption|Percentage Difference)/i.test(line)) {
                line = '### ' + line;
            }
        }
        
        outLines.push(line);
      }
    }
    
    md = outLines.join('\n');
    console.log(md.substring(0, 2000)); // Print start
    
  } catch(e) {
    console.error(e);
  } finally {
    await client.close();
  }
}
main();